Introduction to Mapping Geographic Data

HES 505 Fall 2023: Session 10

Matt Williamson

Today’s Plan

Objectives

  • By the end of today, you should be able to:

  • Describe the basic components of data visualization as a foundation for mapping syntax

  • Understand layering in both base plot and tmap

  • Make basic plots of multiple spatial data objects

Using plot

Which packages have plot methods?

  • Often the fastest way to view data

  • Use ?plot to see which packages export a method for the plot function

  • Or you can use ?plot.*** to see which classes of objects have plot functions defined

plot for sf objects

  • Can plot outlines using plot(st_geometry(your.shapfile)) or plot(your.shapefile$geometry)

  • Plotting attributes requires “extracting” the attributes (using plot(your.shapefile["ATTRIBUTE"]))

  • Controlling aesthetics can be challenging

  • layering requires add=TRUE

plot for sf objects

plot(st_geometry(cejst))

plot(cejst["EALR_PFS"])

plot for SpatRasters

plot(rast.data)

plot for SpatRasters

plot(rast.data["WHP_ID"], col=heat.colors(24, rev=TRUE))

Combining the two with add=TRUE

plot(rast.data["WHP_ID"], col=heat.colors(24, rev=TRUE))
plot(st_geometry(st_transform(cejst, crs=crs(rast.data))), add=TRUE)

Thinking about map construction

Grammar of Graphics (Wilkinson 2005)

  • Grammar: A set of structural rules that help establish the components of a language

  • System and structure of language consist of syntax and semantics

  • Grammar of Graphics: a framework that allows us to concisely describe the components of any graphic

  • Follows a layered approach by using defined components to build a visualization

  • ggplot2 is a formal implementation in R

Aesthetics: Mapping Data to Visual Elements

  • Define the systematic conversion of data into elements of the visualization

  • Are either categorical or continuous (exclusively)

  • Examples include x, y, fill, color, and alpha

From Wilke 2019

Scales

  • Scales map data values to their aesthetics

  • Must be a one-to-one relationship; each specific data value should map to only one aesthetic

Adding aesthetics with tmap

Using tmap

library(sf)
library(terra)
library(tmap)
pt <- tm_shape(cejst) + 
  tm_polygons(col = "EALR_PFS",
              border.col = "white") + 
  tm_legend(outside = TRUE)

Using tmap

Changing aesthetics

pt <- tm_shape(cejst) + 
  tm_polygons(col = "EALR_PFS", n=10,palette=viridis(10),
              border.col = "white") + 
  tm_legend(outside = TRUE)

Changing aesthetics

Adding layers

ORDER MATTERS

st <- tigris::states(progress_bar=FALSE) %>% filter(STUSPS %in% c("ID", "WA", "OR")) %>% st_transform(., crs = st_crs(cejst))
pt <- tm_shape(cejst) + 
  tm_polygons(col = "EALR_PFS", n=10,palette=viridis(10),
              border.col = "white") + 
  tm_shape(st) +
  tm_borders("red") +
  tm_legend(outside = TRUE)

Adding layers

Integrating Rasters

cejst.proj <- st_transform(cejst, crs=crs(rast.data)) %>% filter(!st_is_empty(.))
states.proj <- st_transform(st, crs=crs(rast.data))
pal8 <- c("#33A02C", "#B2DF8A", "#FDBF6F", "#1F78B4", "#999999", "#E31A1C", "#E6E6E6", "#A6CEE3")
pt <- tm_shape(rast.data["category"]) +
  tm_raster(palette = pal8) +
  tm_shape(cejst.proj) + 
  tm_polygons(col = "EALR_PFS", n=10,palette=viridis(10),
              border.col = "white") + 
  tm_shape(states.proj) +
  tm_borders("red") +
  tm_legend(outside = TRUE)

Integrating Rasters