
tmap example: topographic map (Africa)
Source:vignettes/examples_topo_Africa.Rmd
examples_topo_Africa.RmdAbout the data
We use several spatial data objects that are contained in tmap. The first is called World, which we only use for country borders. We’ll only select the African countries:
metroAfrica = sf::st_intersection(metro, World[World$continent == "Africa", ])
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries
Africa = World[World$continent == "Africa", ]The second is land which is a stars object that contains land cover. The third object is metro an sf object with metropolitan areas. The fourth and final object is World_rivers.
Topographic map
tm_shape(land) +
tm_raster("cover_cls",
col.legend = tm_legend("Land use")) +
tm_shape(World_rivers) +
tm_lines(
lwd = "strokelwd",
lwd.scale = tm_scale_asis(values.scale = .5),
col = "#A6CEE3") +
tm_shape(Africa, is.main = TRUE) +
tm_borders() +
tm_shape(metroAfrica) +
tm_symbols(fill = "red", shape = "pop2020", size = "pop2020",
size.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values = tm_seq(0.2, 2, power = "sqrt")
),
size.legend = tm_legend("Population in 2020"),
shape.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values = c(21, 23, 22, 21, 23, 22)
),
shape.legend = tm_legend_combine("size")) +
tm_labels("name", priority = "pop2020", size = 0.8,
options = opt_tm_labels(halo = TRUE, halo.col = "white")) +
tm_credits("United Nations, Department of Economic and Social Affairs, Population Division (2014). World Urbanization Prospects.\nProduction of Global Land Cover Data - GLCNMO2008.", position = tm_pos_out("center", "bottom"))
Breaking down in layers
If this code chunk is overwhelming, let’s take a look at the layer groups:
Adding rivers
tm = tm +
tm_shape(World_rivers) +
tm_lines(
lwd = "strokelwd",
lwd.scale = tm_scale_asis(values.scale = .5),
col = "#A6CEE3")
tm
African countries
Note that the first two layers are from a global data set (so not just Africa).
Note that in the final plot this raster is cropped to the bounding box of Africa.
We’ll use is.main for the spatial object Africa to do the actual cropping.
tm = tm +
tm_shape(Africa, is.main = TRUE) +
tm_borders()
tm
African cities
The cities are labeled with tm_labels(). Each label is placed at a free position around its city; the largest cities are labeled first (priority), and labels that do not fit are dropped. A white halo makes the labels readable on top of the land cover (see the article on label placement).
tm = tm +
tm_shape(metroAfrica) +
tm_symbols(fill = "red", shape = "pop2020", size = "pop2020",
size.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values = tm_seq(0.2, 2, power = "sqrt")
),
size.legend = tm_legend("Population in 2020"),
shape.scale = tm_scale_intervals(
breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6,
values = c(21, 23, 22, 21, 23, 22)
),
shape.legend = tm_legend_combine("size")) +
tm_labels("name", priority = "pop2020", size = 0.8,
options = opt_tm_labels(halo = TRUE, halo.col = "white"))
tm
See the vignette about scales and about legends.
