::p_load(sf, sfdep, tmap, tidyverse) pacman
In-class Exercise 6: Spatial Weights and Applications
1.1 Overview
1.2 Getting Started
1.2.1 Installing and Loading the R Packages
1.3 The Data
1.3.1 Importing geospatial data
<- st_read(dsn = "data/geospatial",
hunan layer = "Hunan")
Reading layer `Hunan' from data source
`C:\valtyl\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
1.3.2 Importing attribute table
<- read_csv("data/aspatial/Hunan_2012.csv") hunan2012
1.3.3 Combining both data frame by using left join
<- left_join(hunan, hunan2012) %>%
hunan_GDPPC select(1:4, 7, 15)
- note: to retain the geospatial properties, the left dataframe must be the sf dataframe (i.e. hunan)
- left_join() is from dplyr
- usually need to specific ‘join by what?’ but there is built in intelligence to identify which column exists in both
- to know which columns to select by, need to run hunan_GDPPC without the select statement first
1.3.4 Plotting 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, Hunan Province",
main.title.position = "center",
main.title.size = 1.0,
legend.height = 0.40,
legend.width = 0.30,
frame = TRUE) +
tm_borders(alpha = 0.5) +
tm_compass(type="8star", size = 2) +
tm_scale_bar() +
tm_grid(alpha =0.2)
- default number of classes for the legend is 5
- alpha at the borders to reduce the intensity of the border
- classification method of quantile is acceptable, can explore other methods for other cases
1.4 Identify area neighbours
1.4.1 Contiguity neighbours method
Queen’s method
<- hunan_GDPPC %>%
cn_queen mutate(nb = st_contiguity(geometry),
.before = 1)
- more about st_contiguity()
- needs the geometry field of POLYGON sf dataframe
- chap 08 8.5.1,
poly2nb()
is used from spdep, but here we are using sfdep - default is queen so dont need to state which method to use
hunan_GDPPC
is sf polygon data and has the geometry columncn_queen
retains the sf polygon and geometry attributes- .before=1 puts the newly created field at the first column of the table
Rook’s method
<- hunan_GDPPC %>%
cn_rook mutate(nb = st_contiguity(geometry, queen = FALSE),
.before = 1)
- spdep can do bishop method, sfdep cannot do bishop method
# geo <- sf_geometry()
1.5 K-Nearest neighbours method
1.6 Distance band method
- sfdep st_dist_band
1.7 Computing contiguity weights
1.7.1 Contiguity weights: Queen’s method
<- hunan_GDPPC %>%
wm_q mutate(nb = st_contiguity(geometry),
wt = st_weights(nb),
.before = 1)
- ^ this code combines 1.4.1 inside, so 1.4.1 is not needed
- the
wt
column of thewm_q
output is now standardised
1.7.2 Contiguity weights: Rook’s method
<- hunan_GDPPC %>%
wm_r mutate(nb = st_contiguity(geometry, queen = FALSE),
wt = st_weights(nb),
.before = 1)
queen
has to be placed beforewt