Skip to contents

Modes

tmap facilitates two output modes:

  • "plot", which produces static maps, and
  • "view" which produces (using the same tmap code) interactive maps (using the JavaScript library Leaflet as backend).

Via the extension package, tmap.mapgl, two interactive new modes are available, "mapbox" and "maplibre", as demonstrated below.

The default mode is "plot":

## current mode
tmap_mode()
#>  tmap modes "plot" - "view"
#>  toggle with `tmap::ttm()`

Switching between modes

## to view mode
tmap_mode("view")
#>  tmap modes "plot" - "view"

## back to plot mode
tmap_mode("plot")
#>  tmap modes "plot" - "view"

The handy function ttm() is used to toggle between the modes:

## to view mode
ttm()
#>  tmap modes "plot" - "view"

## back to plot mode
ttm()
#>  tmap modes "plot" - "view"

When more than two modes are loaded, rtm() can be used to rotate between modes (see [below(https://r-tmap.github.io/tmap/articles/basics_modes#modes-mapbox-and-maplibre-)]).

Mode "plot"

We start with creating the plot and assign it to a variable called tm.

tm = tm_shape(World, crs = 8857) +
  tm_polygons(
    fill = "press",
    fill.scale = tm_scale_intervals(values = "pu_gn")) +
tm_shape(metro) +
  tm_bubbles(
    size = "pop2020",
    fill = "gold",
    size.scale = tm_scale_continuous(values.scale = 0.8, n = 8))

We are in "plot" mode. Now we can plot the map by printing the tm object:

tm

Mode "view"

The same map in view mode:

ttm()
#>  tmap modes "plot" - "view"
tm

Note that there is a big difference: in "view" mode there are basemaps, and in "plot" mode none. This is caused by different default options.

Basemaps can be enabled or disabled via tm_basemap():

tm + tm_basemap(NULL)
#> [view mode] WebGL does not work (yet) with projected map projections, so it has
#> been disabled.
#> This message is displayed once per session.