install.packages("tmap.mapgl")
# Or development version:
remotes::install_github("r-tmap/tmap.mapgl")Session 7: Introduction to tmap.mapgl
| Mode | Package | Backend | JS library |
|---|---|---|---|
"plot" |
tmap | grid | (none) |
"view" |
tmap | leaflet | Leaflet |
"mapbox" |
tmap.mapgl | mapgl | Mapbox GL JS |
"maplibre" |
tmap.mapgl | mapgl | MapLibre GL JS |
The "view" mode (Leaflet) is great for most interactive maps. The mapgl modes extend this with large-data performance, 3D visualisation, and a globe CRS.
For most purposes during this course, MapLibre is the preferred choice.
✅ Advantages over "view" mode:
"view" via leafgl, but more restricted)tm_polygons_3d() — Session 8⚠️ Trade-offs:
"mapbox" requires a free API keytmap.mapgl depends on the mapgl package, which is installed automatically.
Once tmap.mapgl is loaded, four modes are available and rtm() rotates through them all.
rtm() and ttm()Get a free key at account.mapbox.com.
tm_maplibre() and tm_mapbox()Just as tm_view() controls Leaflet options, use tm_maplibre() / tm_mapbox() for mapgl-specific options:
tmap_providers()In the mapgl modes, background maps are set via tm_basemap() — the same as in "view" mode. The available providers are queried with tmap_providers():
MapLibre/Mapbox JS use the term “style” for their background maps. In tmap, these are exposed as basemaps via
tm_basemap()— keeping them separate from tmap’s own style system (tmap_style()).
The default CRS in "maplibre" mode is the globe — no projection distortion. This makes it ideal for world maps:
In the map viewer, two controls appear at the top right:
These controls let users switch projection and orientation interactively.
The same map specification works across modes — just switch the mode:
"mapbox" and "maplibre" modes to tmaplibrary(tmap.mapgl) — no other changes needed"maplibre" is free; "mapbox" requires a free API keyrtm() to rotate through all available modes; ttm() toggles between current and previoustm_basemap() + tmap_providers() (not style = anymore)tm_polygons_3d() (Session 8)