In-class Exercise 6

Published

February 13, 2023

Modified

April 19, 2023

1 Load packages

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

2 The data

2.1 Importing Geospatial data

hunan <- st_read(dsn = "data/geospatial/",
                 layer = "Hunan")
Reading layer `Hunan' from data source 
  `/Users/junhaoteo/Documents/junhao2309/IS415/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

2.2 Importing Aspatial data

hunan_2012 <- read_csv("data/aspatial/Hunan_2012.csv")
hunan_GDPPC <- dplyr::left_join(hunan, hunan_2012) %>%
  select(1:4, 7, 15)

2.3 Visualize the data on a Choropleth Map

tmap_mode("plot")
tm_shape(hunan_GDPPC) +
  tm_fill("GDPPC",
          style = "quantile",
          palette = "Blues",
          title = "GDPPC") +
  tm_layout(main.title = "Distribution of GDP per capita by district",
            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)

3 Identify area neighbours

3.1 Contiguity neighbours method

In the code chunk below, st_contiguity() is used to derive a contiguity neighbour list by using Queen’s method. By default, st_contiguity() uses queen = TRUE.

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

The code chunk below uses st_contiguity() with queen = FALSE. This makes the contiguity neighbor list using the Rook’s method.

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

3.2 Computing contiguity weights

3.2.1 Queen’s method

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

3.2.2 Rook’s method

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