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 required to use "mapbox"
(free
for personal use), but "maplibre"
is (as the name suggests)
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()
#> ℹ tmap modes "plot" -> "view" -> "mapbox" -> "maplibre"; rotate with
#> `tmap::rtm()`; switch to "plot" with `tmap::ttm()`
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 modes "plot" -> "view" -> "mapbox" -> "maplibre"; rotate with
#> `tmap::rtm()`; switch to "plot" with `tmap::ttm()`
map
#> [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
#> This message is displayed once per session.
tmap_mode("view")
#> ℹ tmap modes "plot" -> "view" -> "mapbox" -> "maplibre"; rotate with
#> `tmap::rtm()`; switch to "plot" with `tmap::ttm()`
map
#> Warning: tm_scale_intervals `label.style = "continuous"` implementation in view mode
#> work in progress
tmap_mode("mapbox")
map

tmap_mode("maplibre")
#> ℹ tmap modes "plot" -> "view" -> "mapbox" -> "maplibre"; rotate with
#> `tmap::rtm()`; switch to "view" with `tmap::ttm()`
map