::p_load(tidyverse, tmap, sf, spdep, sfdep) pacman
In-Class Exercise 06
Installing packages
Importing geospatial data
<- st_read("data/geospatial/",
hunan 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
<- read_csv("data/aspatial/Hunan_2012.csv") hunan2012
Combining Hunan sf dataframe and Hunan_2012 dataframe
If want to retain the geometry, have to join the left side of the data frame
<- left_join(hunan,hunan2012)%>% #will auto join by a matching column
hunan_GDPPC 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
<- hunan_GDPPC %>%
cn_queen mutate(nb= st_contiguity(geometry),
.before = 1)
Contiguity neighbour list using Rook’s method
<- hunan_GDPPC %>%
cn_rook mutate(nb = st_contiguity(geometry),
queen = FALSE,
.before = 1)
Computing contiguity weights
Queen’s method - combining weights
<- hunan_GDPPC %>%
wm_q mutate(nb = st_contiguity(geometry),
wt = st_weights(nb),
.before = 1)