In-Class Exercise 6:

Published

February 13, 2023

Modified

March 17, 2023

Getting started

Importing Packages

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

Importing Shapefile Data

hunan <- st_read(dsn = "data/geospatial", 
                 layer = "Hunan")
Reading layer `Hunan' from data source 
  `C:\Users\la935\Desktop\IS415 - GAA\IS415 - GAA (New)\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 CSV file

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

Combining Both Data Frame by using left join

hunan_GDPPC <- left_join(hunan,hunan2012)%>%
  select(1:4, 7, 15)

Visualising

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 distribution",
            main.title.position = "center",
            main.title.size = 0.6,
            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)

Contiguity Neighbor method

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

Derive 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

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

Rooks method

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