Skip to contents

Recall that tmap offers two modes: "plot" for static mapping and "view" for interactive mapping. See introduction. The "view" mode uses the JavaScript library Leaflet as backend.

The extension package tmap.mapgl offers two new modes which are also interactive: "mapbox" and "maplibre" which use the JavaScript libraries Mapbox GL and Maplibre GL respectively. An API key is requires to use "mapbox" (free for personal use), but "maplibre" is (as the name suggestions) free for any use.

Note that tmap.mapgl is a bridge between the R packages mapgl and tmap. It makes the functionality of mapgl (making the JavaScript libraries available to R) also available via the tmap user interface.

Switching modes

Get the current mode

tmap_mode()
#>  Current tmap mode is "plot".
#>  Call `tmap::ttm()` to switch to mode "view".
#>  Call `tmap::rtm()` to rotate between all modes: "plot" -> "view" -> "mapbox"
#>   -> "maplibre"

For this example we’ll create a choropleth of well being per country. We assign the map to map without showing it.

map = tm_shape(World) + 
  tm_polygons("well_being",
    fill.scale = tm_scale_continuous(values = "pu_gn"))

Now we’ll show the map in all (now four) modes

The modes

tmap_mode("plot")
#>  tmap mode set to "plot".
map
#> [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
#> [plot mode] fit legend/component: Some legend items or map compoments do not
#> fit well, and are therefore rescaled.
#>  Set the tmap option `component.autoscale = FALSE` to disable rescaling.
#> This message is displayed once per session.

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