knitr::opts_chunk$set( collapse = TRUE, out.width = "100%", dpi = 300, fig.width = 7.2916667, comment = "#>" ) hook_output <- knitr::knit_hooks$get("output") knitr::knit_hooks$set(output = function(x, options) { lines <- options$output.lines if (is.null(lines)) { return(hook_output(x, options)) # pass to default hook } x <- unlist(strsplit(x, "\n")) more <- "..." if (length(lines)==1) { # first n lines if (length(x) > lines) { # truncate the output, but add .... x <- c(head(x, lines), more) } } else { x <- c(more, x[lines], more) } # paste these lines together x <- paste(c(x, ""), collapse = "\n") hook_output(x, options) })
library(tmap) library(dplyr) library(sf) tmap_options(scale = 0.75)
We use several spatial data object 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", ]) 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 ovject is World_rivers
.
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.range = c(0.2,2) ), 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") + 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"))
If this code chunk is overwhelming, let's that a look at the layer groups:
tm = tm_shape(land) + tm_raster("cover_cls", col.legend = tm_legend("Land use")) tm
tm = tm + tm_shape(World_rivers) + tm_lines( lwd = "strokelwd", lwd.scale = tm_scale_asis(values.scale = .5), col = "#A6CEE3") tm
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 the spatial object Africa
to do the actual cropping.
tm = tm + tm_shape(Africa, is.main = TRUE) + tm_borders() tm
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.range = c(0.2,2) ), 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") tm
See the vigette about scale and about legends.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.