Chapter 3 Basics of Handling Spatial Data in R

3.1 Overview

3.1.1 Spatial Data

3.2 Introduction to sp and sf: the sf revolution

3.2.1 sp data format

3.2.1.1 Spatial data in GISTools

The New Haven census blocks areas with roads in blue and the counties in the state of Georgia shaded by median income

Figure 3.1: The New Haven census blocks areas with roads in blue and the counties in the state of Georgia shaded by median income

[1] "blocks"    "breach"    "burgres.f" "burgres.n"
[5] "famdisp"   "places"    "roads"     "tracts"   
[1] "SpatialPoints"
attr(,"package")
[1] "sp"
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
The New Haven census blocks and road data

Figure 3.2: The New Haven census blocks and road data

3.4 Mapping: an introduction to tmap

3.4.1 Introduction

3.4.3 Full tmap

Counties in the state of Georgia

Figure 3.5: Counties in the state of Georgia

Examples of the use of `tmap` to generate multiple maps in the same plot window

Figure 3.6: Examples of the use of tmap to generate multiple maps in the same plot window

Adding text to map objects with `tmap`

Figure 3.7: Adding text to map objects with tmap

A subset of the counties in the state of Georgia

Figure 3.8: A subset of the counties in the state of Georgia

The result of the code for plotting a spatial object and a spatial subset

Figure 3.9: The result of the code for plotting a spatial object and a spatial subset

3.5 Mapping spatial data attributes

3.5.1 Introduction

3.5.3 Mapping polygons and attributes

[1] "#EFF3FF" "#BDD7E7" "#6BAED6" "#3182BD" "#08519C"
Different chroropleth maps of owner occupied properties in New Haven using different shades and class intervals.

Figure 3.11: Different chroropleth maps of owner occupied properties in New Haven using different shades and class intervals.

An illustration of the various options for mapping with `tmap`.

Figure 3.12: An illustration of the various options for mapping with tmap.

3.5.4 Mapping points and attributes

A plot of the Fiji earthquake data.

Figure 3.13: A plot of the Fiji earthquake data.

Legend labels were too wide. Therefore, legend.text.size has been set to 0.5. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
The legend is too narrow to place all symbol sizes.
Plotting points with  plot size (left) and plot colour (right) related to the atrribute value.

Figure 3.14: Plotting points with plot size (left) and plot colour (right) related to the atrribute value.

Plotting points with a Google map context

Figure 3.15: Plotting points with a Google map context

Plotting points with Google satellite image context

Figure 3.16: Plotting points with Google satellite image context

3.6 Simple descriptive statistical analyses

3.6.2 Scatterplots and Regressions


Call:
lm(formula = p.vac ~ p.w, data = df)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.11747 -0.03729 -0.01199  0.01714  0.28271 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.11747    0.01092  10.755   <2e-16 ***
p.w         -0.03548    0.01723  -2.059   0.0415 *  
---
Signif. codes:  
0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.06195 on 127 degrees of freedom
Multiple R-squared:  0.03231,   Adjusted R-squared:  0.02469 
F-statistic:  4.24 on 1 and 127 DF,  p-value: 0.04152
Plotting regression coefficient slopes

Figure 3.20: Plotting regression coefficient slopes

3.7 Self-Test Questions

Self-Test Question 1 - Plots and maps: working with map data

Self-Test Question 2 - Mis-Representation of continuous variables: using different cutters for choropleth mapping

Self-Test Question 3 - Selecting data: creating variables and subsetting data using logical statements

Self-Test Question 4 - Rransformations data using spTransfrom and st_transform

3.8 Answers to self-test questions

Q1. Plots and maps: working with map data.

The map produced by the code for Q1

Figure 3.23: The map produced by the code for Q1

Q2. Mis-Representation of continuous variables - using different cutters for choropleth mapping.

# load packages and data
library(tmap)
library(GISTools)
library(sf)
library(grid)
data(newhaven)
# convert data to sf format
  blocks_sf = st_as_sf(blocks)
# 1. Initial Investigation
# You could start by having a look at the data
attach(data.frame(blocks_sf))
hist(HSE_UNITS, breaks = 20)
# You should notice that it has a normal distribution 
# but with some large outliers 
# Then examine different cut schemes
quantileCuts(HSE_UNITS, 6)
rangeCuts(HSE_UNITS, 6)
sdCuts(HSE_UNITS, 6)
# detach the data frame
detach(data.frame(blocks_sf))
# 2. Do the task 
# a) mapping classes defined by quantiles
# define some breaks
br <- c(0, round(quantileCuts(blocks_sf$HSE_UNITS, 6),0))
# you could examine br
p1 <- tm_shape(blocks_sf) +
  tm_polygons("HSE_UNITS", title = "Quantiles", 
      palette = "Reds",
      breaks = br)
# b) mapping classes defined by absolute ranges
# define some breaks
br <- c(0, round(rangeCuts(blocks$HSE_UNITS, 6),0))
# you could examine br
p2 <- tm_shape(blocks_sf) +
  tm_polygons("HSE_UNITS", title = "Ranges", 
      palette = "Reds",
      breaks = br)
# c) mapping classes defined by standard deviations
br <- c(0, round(sdCuts(blocks$HSE_UNITS, 6),0))
# you could examine br
p3 <- tm_shape(blocks_sf) +
  tm_polygons("HSE_UNITS", title = "Std Dev", 
      palette = "Reds",
      breaks = br)
# open a new plot page
grid.newpage()
# set up the layout
pushViewport(viewport(layout=grid.layout(1,3)))
# plot using he print command
print(p1, vp=viewport(layout.pos.col = 1, height = 5))
print(p2, vp=viewport(layout.pos.col = 2, height = 5))
print(p3, vp=viewport(layout.pos.col = 3, height = 5))
The map produced by the code for Q2

Figure 3.24: The map produced by the code for Q2

Q3 Selecting data - creating variables and subsetting data using logical statements.

The map produced by the code for Q3

Figure 3.25: The map produced by the code for Q3

Q4 Transforming data. Your map should look something like figure or figure depending on which way you did it!

The OSM map produced by the code for Q4

Figure 3.26: The OSM map produced by the code for Q4