Key Learnings from, and Solutions to the exercises in Chapter 8 of the book Geocomputation with R by Robin Lovelace, Jakub Nowosad and Jannes Muenchow.
Geocomputation with R
Textbook Solutions
Author
Aditya Dahiya
Published
March 2, 2025
In this chapter, I use {ggplot2}(Wickham 2016) to produce equivalent maps, as produced by {tmap}(Tennekes 2018) in the textbook. In addition, I use {cols4all}(Tennekes and Puts 2023) palettes for colour and fill scales.
Code
library(sf) # Simple Features in Rlibrary(terra) # Handling rasters in Rlibrary(tidyterra) # For plotting rasters in ggplot2library(magrittr) # Using pipes with raster objectslibrary(tidyverse) # All things tidy; Data Wranglinglibrary(spData) # Spatial Datasetslibrary(patchwork) # Composing plotslibrary(gt) # Display GT tables with R
9.1 Introduction
Cartography is a crucial aspect of geographic research, blending communication, detail, and creativity.
Static maps in R can be created using the plot() function, but advanced cartography benefits from dedicated packages.
The chapter focuses in-depth on the tmap package rather than multiple tools superficially.
Code
# install.packages("colorblindcheck")# install.packages("cols4all")# A nice way to pick colour palettes for maps etc.# cols4all::c4a_gui()cols4all::c4a_palettes()
9.2 Static maps
Most common type of geo-computation output, stored as .png (raster) and .pdf (vector).
Base R’s plot() is the fastest way to create static maps from sf or terra, ideal for quick visual checks.
Grammar of graphics: Like ggplot2, tmap follows a structured approach, separating input data from aesthetics (visual properties).
Basic structure: Uses tm_shape() to define the input dataset (vector or raster), followed by layer elements like tm_fill() and tm_borders().
Layering approach:
tm_fill(): Fills (multi)polygon areas.
tm_borders(): Adds border outlines to (multi)polygons.
tm_polygons(): Combines fill and border.
tm_lines(): Draws lines for (multi)linestrings.
tm_symbols(): Adds symbols for points, lines, and polygons.
tm_raster(): Displays raster data.
tm_rgb(): Handles multi-layer rasters.
tm_text(): Adds text labels.
Layering operator: The + operator is used to add multiple layers.
Quick maps: qtm() provides a fast way to generate thematic maps (qtm(nz) ≈ tm_shape(nz) + tm_fill() + tm_borders()).
Limitations of qtm(): Less control over aesthetics, so not covered in detail in this chapter.
9.2.2 Map objects
{tmap} allows storing maps as objects, enabling modifications and layer additions.
Use tm_polygons() to create a map object, combining tm_fill() and tm_borders().
Stored maps can be plotted later by simply calling the object.
Additional layers are added using + tm_shape(new_obj), where new_obj represents a new spatial object.
Aesthetic functions apply to the most recently added shape until another is introduced.
Spatial objects can be manipulated with sf, e.g., st_union(), st_buffer(), and st_cast().
Multiple layers can be added, such as:
Raster elevation (tm_raster())
Territorial waters (tm_lines())
High points (tm_symbols())
tmap_arrange() combines multiple tmap objects into a single visualization.
The + operator adds layers, but aesthetics are controlled within layer functions.
9.2.3 Visual variables
Default aesthetics in tmap:
tm_fill() and tm_symbols() use gray shades.
tm_lines() uses a continuous black line.
Defaults can be overridden for customization.
Types of map aesthetics:
Variable-dependent aesthetics (change with data).
Fixed aesthetics (constant values).
Key aesthetic arguments in tmap:
fill: Polygon fill color.
col: Border, line, point, or raster color.
lwd: Line width.
lty: Line type.
size: Symbol size.
shape: Symbol shape.
fill_alpha, col_alpha: Transparency for fill and border.
Applying aesthetics:
Use a column name to map a variable. Pass a character string referring to a column name.
Use a fixed value for constant aesthetics.
Additional arguments for visual variables:
.scale: Controls representation on the map and legend.
.legend: Customizes legend settings.
.free: Defines whether each facet uses the same or different scales.
9.2.4 Scales
Scales define how values are visually represented in maps and legends, depending on the selected visual variable (e.g., fill.scale, col.scale, size.scale).
Default scale is tm_scale(), which auto-selects settings based on input data type (factor, numeric, integer).