Chapter 5 tmap

Harold Nelson

2023-03-01

We’re going to see how to redo the ggplot2 maps in Chapter 5 using tmap.

Setup

library(sf)           
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(rgdal)        
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.2, released 2022/03/08
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: FALSE 
## Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-7
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(ggplot2)      
library(dplyr)        
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)        
library(scales)       
library(RColorBrewer) 
library(units)
## udunits database from /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/units/share/udunits/udunits2.xml
library(cowplot)
library(tmap)
library(tmaptools)

Data Construction

This chunk includes importing and restructuring the data used in Chapter 5.

okcounty <- st_read("ok_counties.shp", quiet = TRUE)
tpoint <- st_read("ok_tornado_point.shp", quiet = TRUE)
tpath <- st_read("ok_tornado_path.shp", quiet = TRUE)
class(okcounty)
## [1] "sf"         "data.frame"
## [1] "sf"         "data.frame"
glimpse(okcounty)
## Rows: 77
## Columns: 8
## $ STATEFP  <chr> "40", "40", "40", "40", "40", "40", "40", "40", "40", "40", "…
## $ COUNTYFP <chr> "077", "025", "011", "107", "105", "153", "001", "053", "059"…
## $ COUNTYNS <chr> "01101826", "01101800", "01101793", "01101841", "01101840", "…
## $ AFFGEOID <chr> "0500000US40077", "0500000US40025", "0500000US40011", "050000…
## $ GEOID    <chr> "40077", "40025", "40011", "40107", "40105", "40153", "40001"…
## $ NAME     <chr> "Latimer", "Cimarron", "Blaine", "Okfuskee", "Nowata", "Woodw…
## $ LSAD     <chr> "06", "06", "06", "06", "06", "06", "06", "06", "06", "06", "…
## $ geometry <POLYGON [°]> POLYGON ((-95.50766 35.0292..., POLYGON ((-103.0025 3…
tpoint_16_21 <- tpoint %>%
  filter(yr >= 2016 & yr <= 2021) %>%
  select(om, yr, date)
tpath_16_21 <- tpath %>%
  filter(yr >= 2016 & yr <= 2021) %>%
  select(om, yr, date)

countypnt <- st_join(tpoint_16_21, okcounty)

countypnt <- st_drop_geometry(countypnt)
countysum <- countypnt %>%
  group_by(GEOID) %>%
  summarize(tcnt = n())

countymap <- okcounty %>%
  left_join(countysum, by = "GEOID") %>%
  replace(is.na(.), 0) %>%
  mutate(area = st_area(okcounty),
         tdens = 10^6 * 10^3 * tcnt / area) %>%
  drop_units()

okcntrd = st_centroid(countymap)
## Warning in st_centroid.sf(countymap): st_centroid assumes attributes are
## constant over geometries of x

The first Map

We did this with ggplot2

ggplot(data = okcounty) +
  geom_sf(fill = NA)

Do it with tmap

tm_shape(okcounty) +
  tm_polygons()

Another ggplot2 map

ggplot() +
  geom_sf(data = okcounty, fill = NA) +
  geom_sf(data = tpoint_16_21) +
  theme_bw()

Do it with tmap.

Solution

tm_shape(okcounty) +
  tm_borders() +
  tm_shape(tpoint_16_21) +
  tm_dots() +
  tm_style("classic")  

Another ggplot2 map

ggplot() +
  geom_sf(data = okcounty, fill = NA) +
  geom_sf(data = tpath_16_21, 
          color = "red", 
          size = 1) +
  theme_bw()

With tmap

tm_shape(okcounty) +
  tm_borders() +
  tm_shape(tpath_16_21) +
  tm_lines(col = "red", lwd = 1) 

With ggplot2

ggplot() +
  geom_sf(data = tpoint_16_21, 
          aes(color = as.factor(yr))) +
  geom_sf(data = okcounty, fill = NA) +
  scale_color_discrete(name = "Year") +
  coord_sf(datum = NA) +
  theme_void()

With tmap

tpoint_16_21_f = tpoint_16_21 %>% 
  mutate(yr = factor(yr))

tm_shape(okcounty) +
  tm_borders() +
  tm_shape(tpoint_16_21_f) +
  tm_dots(col = "yr") +
  tm_layout(frame = F)

Another ggplot2 Map

ggplot() +
  geom_sf(data = okcounty, 
          fill = NA, 
          color = "gray") +
  geom_sf(data = tpoint_16_21, size = 0.75) +
  facet_wrap(vars(yr), ncol = 2) +
  coord_sf(datum = NA) +
  theme_void()

With tmap

tm_shape(okcounty) +
  tm_polygons(border.col = "gray", border.alpha = 0.5) +
  tm_shape(tpoint_16_21) +
  tm_dots(size = 0.75) +
  tm_facets(by = "yr", ncol = 2) +
  tm_layout(frame = FALSE)

Another ggplot2 map

ggplot(data = countymap) +
  geom_sf(aes(fill = tdens)) +
  theme_void()

Do it with tmap.

tm_shape(countymap) +
  tm_polygons("tdens", palette = "Blues", style = "quantile") +
  tm_layout(frame = FALSE) +
  tm_compass()

Another Map with ggplot2

ggplot() +
  geom_sf(data = okcntrd, aes(size = tcnt)) +
  geom_sf(data = okcounty, fill = NA) +
  theme_void()

Do it with tmap

tm_shape(okcounty) +
  tm_borders() +
  tm_shape(okcntrd) +
  tm_symbols(size = "tcnt", shape = 21, border.col = "black") +
  tm_layout(frame = FALSE) +
  tm_compass()

Another ggplot2 Map

ggplot(data = countymap) +
  geom_sf(aes(fill = tdens)) +
  scale_fill_distiller(name = expression("Tornadoes/1000 km"^2), 
                       palette = "YlOrRd", 
                       breaks = pretty_breaks(),
                       direction = 1) +
  theme_void() +
  theme(legend.position = "bottom")

Do it with tmap

tm_shape(countymap) +
  tm_polygons("tdens", palette = "YlOrRd", 
              style = "quantile", n = 5, 
              title = expression("Tornadoes/1000 km"^2),
              legend.show = TRUE) +
  tm_layout(frame = FALSE)