Required packages

library(tmap)
library(sf)
library(dplyr)

tmap_mode("view")   # all maps in this exercise set are interactive

Datasets used — all from the tmap package, no downloads needed.

Object Rows Type Key variables
World 177 polygon continent, gdp_cap_est, life_exp, well_being, pop_est_dens, economy
World_rivers 1511 line scalerank
metro 436 point pop1960pop2030, name
NLD_prov 12 polygon code, name (boundary only)
NLD_muni 345 polygon population, dwelling_value, dwelling_ownership, employment_rate, income_low, income_high, edu_appl_sci, urbanity, province
NLD_dist 3340 polygon same variables as NLD_muni, district level

Exercise 1 — World data

1.1 — First interactive map

  1. Type ?World to find out what variables it contains. Make a (static) map of World, where the polygons have white border lines (line width 2) and are filled with purple.
# your code here
  1. Switch to "view" mode and reproduce the map. Pan, zoom, and click a country — what happens?
# your code here
  1. Create an interactive choropleth of life expectancy by setting fill to the variable name "life_exp". Experiment with country border lines — which colour and line width work well?
# your code here
  1. Add tm_crs("auto") to apply an equal-area projection, and tm_basemap(NULL) to disable the basemap. Compared to the map in (c), which do you prefer and why?
# your code here

1.2 — Stacking layers

  1. Build a map with three layers: World (grey fill, no borders), World_rivers (blue lines), and metro (gold bubbles). Click the layer control icon (top-left in the viewer) to toggle layers on and off. Experiment with line thickness and bubble size.
# your code here
  1. The World_rivers dataset contains a variable "strokelwd" with pre-computed line widths. Assign it to lwd and set lwd.scale = tm_scale_asis() so the values are used directly rather than treated as a data variable.
# your code here — build on (a)
  1. The metro dataset contains population estimates per city, e.g. "pop2020". Assign this to size in tm_symbols().
# your code here — build on (b)
  1. Give each layer a meaningful name using the group argument in the layer function (e.g. tm_fill(..., group = "Land")). Check that the names appear correctly in the layer control.
# your code here — build on (c)

Exercise 2 — Netherlands data

2.1 — Three scales

  1. Load NLD_prov, NLD_muni, and NLD_dist. Make three separate interactive maps — one per dataset — each filled with "steelblue". Zoom in and out on each to appreciate the resolution difference.
data("NLD_prov"); data("NLD_muni"); data("NLD_dist")

# provinces

# municipalities

# districts
  1. Which are the richest districts? Create a choropleth of "dwelling_value" at district level.
# your code here
  1. Improve this map by adding thick province borders and less-thick municipality borders as additional layers, and removing the district borders (col = NULL). Which border thicknesses and colours work well?
# your code here — build on (b)
  1. Compare tm_scale_intervals() and tm_scale_continuous_pseudo_log(). Make one map with each and explore their arguments (e.g. style and n for the interval scale, or manual breaks). Which do you prefer and why?
# map 1 — tm_scale_intervals
# map 2 — tm_scale_continuous_pseudo_log
  1. Explore the sequential and diverging colour palettes in cols4all:
library(cols4all)
c4a_gui()
  1. Apply your preferred palette to the map from (d) by setting values = "<palette name>" inside the scale function.
# your code here — build on (d)
  1. Change the legend title to something meaningful (e.g. "WOZ value (×€1 000)") and move the legend to the top-right corner. Do this with the fill.legend argument: tm_legend(title = "...", position = c("right", "top")).
# your code here — build on (f)
  1. Add a scale bar using tm_scalebar().
# your code here — build on (g)
  1. Add a basemap of your choice. Browse the options at https://leaflet-extras.github.io/leaflet-providers/preview/ and add your chosen basemap with tm_basemap("<provider name>") at the top of the map. Which basemap complements the choropleth best?
# your code here — build on (h)