Skip to contents

With the tmap package, thematic maps can be generated with great flexibility. The syntax for creating plots is similar to that of ggplot2, but tailored to maps. This vignette is for those who want to get started with tmap within a couple of minutes.

For more context on R’s geographic capabilities we recommend the online version of the book Geocomputation with R. The Making maps with R chapter of the book provides many more context and abundant code examples of map making with tmap and other packages. Other good resources are the vignettes of the sf package.

Hello World!

A good place to start is to create a map of the world. After installing tmap, the following lines of code should create the map shown below:

library(tmap)
tm_shape(World) +
    tm_polygons("HPI")

The object World is a spatial object of class sf from the sf package; it is a data.frame with a special column that contains a geometry for each row, in this case polygons. In order to plot it in tmap, you first need to specify it with tm_shape(). Layers can be added with the + operator, in this case, tm_polygons(). There are many layer functions in tmap, which can easily be found in the documentation by their tm_ prefix. See also ?'tmap-element'.

Interactive maps

Each map can be plotted as a static image or viewed interactively using "plot" and "view" modes, respectively. The mode can be set with the function tmap_mode, and toggling between the modes can be done with the ‘switch’ ttm() (which stands for toggle thematic map).

tmap_mode("view")
#>  tmap mode set to "view".

tm_shape(World) +
    tm_polygons("HPI")
#> Registered S3 method overwritten by 'jsonify':
#>   method     from    
#>   print.json jsonlite
#> Warning in checkDim(x, data): Length of color vector does not match number of data rows.
#>   The vector is repeated to match the number of rows.

Multiple shapes and layers

A shape is a spatial object (with a class from sf, stars, or terra). Multiple shapes and also multiple layers per shape can be plotted:


tmap_mode("plot")
#>  tmap mode set to "plot".
tm_shape(land) +
    tm_raster("elevation", palette = terrain.colors(10)) +
tm_shape(World) +
    tm_borders("white", lwd = .5) +
    tm_text("iso_a3", size = "AREA") +
tm_shape(metro) +
    tm_symbols(col = "red", size = "pop2020", scale = .5) +
tm_legend(show = FALSE)
#> 
#> ── tmap v3 code detected ───────────────────────────────────────────────────────
#> [v3->v4] `tm_raster()`: migrate the argument(s) related to the scale of the
#> visual variable `col` namely 'palette' (rename to 'values') to col.scale =
#> tm_scale(<HERE>).[v3->v4] `symbols()`: use 'fill' for the fill color of polygons/symbols
#> (instead of 'col'), and 'col' for the outlines (instead of 'border.col').[v3->v4] `symbols()`: migrate the argument(s) related to the scale of the
#> visual variable `size` namely 'scale' (rename to 'values.scale') to size.scale
#> = tm_scale_continuous(<HERE>).
#>  For small multiples, specify a 'tm_scale_' for each multiple, and put them in
#>   a list: 'size.scale = list(<scale1>, <scale2>, ...)'[v3->v4] `tm_legend()`: use 'tm_legend()' inside a layer function, e.g.
#> 'tm_polygons(..., fill.legend = tm_legend())'
#> Warning: Unable to warp stars. Stars will be transformed now (which will take
#> some time).

Facets

Facets can be created in three ways:

  1. By assigning multiple variable names to one aesthetic (in this example the first argument of tm_polygons():
tmap_mode("view")
#>  tmap mode set to "view".
tm_shape(World) +
    tm_polygons(c("HPI", "economy")) +
    tm_facets(sync = TRUE, ncol = 2)
#> Warning in checkDim(x, data): Length of color vector does not match number of data rows.
#>   The vector is repeated to match the number of rows.
#> Warning in checkDim(x, data): Length of color vector does not match number of data rows.
#>   The vector is repeated to match the number of rows.