In-Class Exercise 06

Author

Xinyee How

Published

February 13, 2023

Modified

March 6, 2023

Installing packages

pacman::p_load(tidyverse, tmap, sf, spdep, sfdep) 

Importing geospatial data

hunan <- st_read("data/geospatial/",
                  layer = "Hunan") %>%
  st_transform(crs = 26392)
Reading layer `Hunan' from data source 
  `C:\xinyeehow\IS415-GAA\In-Class_Ex\In-class_Ex06\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 88 features and 7 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 108.7831 ymin: 24.6342 xmax: 114.2544 ymax: 30.12812
Geodetic CRS:  WGS 84

Importing aspatial data

hunan2012 <- read_csv("data/aspatial/Hunan_2012.csv")

Combining Hunan sf dataframe and Hunan_2012 dataframe

If want to retain the geometry, have to join the left side of the data frame

hunan_GDPPC <- left_join(hunan,hunan2012)%>% #will auto join by a matching column
  select(1:4, 7, 15) #selecting columns to retain

Plotting chloropleth map

tmap_mode("plot") #static map, if want interactive->tmap_mode("view")
tm_shape(hunan_GDPPC)+
  tm_fill("GDPPC", 
          style = "quantile", 
          palette = "Blues",
          title = "GDP Per Capita") +
  tm_layout(main.title = "Distribution of GDP per capita in Hunan",
            main.title.position = "center",
            main.title.size = 1.2,
            legend.height = 0.45, 
            legend.width = 0.35,
            frame = TRUE) +
  tm_borders(alpha = 0.5) +
  tm_compass(type="8star", size = 2) +
  tm_scale_bar() +
  tm_grid(alpha =0.2)

Identify area neighbours

Calculate contiguity neighbours

cn_queen <- hunan_GDPPC %>%
  mutate(nb= st_contiguity(geometry),
        .before = 1)

Contiguity neighbour list using Rook’s method

cn_rook <- hunan_GDPPC %>%
  mutate(nb = st_contiguity(geometry),
         queen = FALSE,
         .before = 1)

Computing contiguity weights

Queen’s method - combining weights

wm_q <- hunan_GDPPC %>%
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb),
         .before = 1)