14  tmap options

library(tmap)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(stars)
#> Loading required package: abind
worldelevation = read_stars("data/worldelevation.tif")
worldvector = read_sf("data/worldvector.gpkg")

14.1 Data simplification

Geometries in spatial vector data consists of sets of coordinates (Section 2.2.1). Spatial vector objects grow larger with more features to present and more details to show, and this also has an impact on time to render a map. Figure 14.1 (a) shows a map of countries from the worldvector object.

tm_shape(worldvector) +
  tm_polygons()

This level of detail can be good for some maps, but sometimes the number of details can make reading the map harder. To create a simplified (smoother) version of vector data, we can use the ms_simplify function of the rmapshaper package. . It expects a numeric value from 0 to 1 – a proportion of vertices in the data to retain. In the example below, we set keep to 0.05, which keeps 5% of vertices (Figure 14.1 (b)).

library(rmapshaper)
worldvector_s1 = ms_simplify(worldvector, keep = 0.05)
tm_shape(worldvector_s1) +
  tm_polygons()

The process of simplification can also be more controlled. By default, the underlining algorithm (called the Visvalingam method, learn more at https://bost.ocks.org/mike/simplify/), removes small features, such as islands in our case. This could have far-reaching consequences - in the process of simplification, we could remove some countries! To prevent the deletion of small features, we also need to set keep_shapes to TRUE. In the case of one country consisting of many small polygons, only one is sure to be retained. For example, look at New Zealand, which is now only represented by Te Waipounamu (the South Island). To keep all of the spatial geometries (even the smallest of islands), we should also specify explode to TRUE.

worldvector_s2 = ms_simplify(worldvector, keep = 0.05,
                             keep_shapes = TRUE, explode = TRUE)
tm_shape(worldvector_s1) +
  tm_polygons()

Figure 14.1 (c) contains a simplified map, where each spatial geometry of the original map still exists, but in a less detailed form.

(a) original data
(b) simplified data with 5% of vertices kept
(c) simplified data with 5% of vertices, all features, and all polygons kept
Figure 14.1: A map of world’s countries based on:

Raster data is represented by a grid of cells (Section 2.2.2), and the number of cells impacts the time to render a map. Rasters with hundreds of cells will be plotted quickly, while rasters with hundreds of millions or billions of cells will take a lot of time (and RAM) to be shown. Therefore, the tmap package downsamples large rasters by default to be below 10,000,000 cells in the plot mode and 1,000,000 cells in the view mode. This values can be adjusted with the raster.max_cells argument of tmap_options(), which expects a named vector with two elements - plot and view (Figure 14.2).

tmap_options(raster.max_cells = c(plot = 5000, view = 2000))
tm_shape(worldelevation) +
  tm_raster("worldelevation.tif")
Figure 14.2: A raster map with the decreased resolution

Any tmap options can be reset (set to default) with tmap_options_reset() (We explain tmap_options() in details in Chapter 14).