Interactive Maps with tmap and Shiny

Session 10: Exporting and sharing interactive maps

Martijn Tennekes

Content

  • Save static plots
  • Save interactive plots
  • Save animations

Saving static plots

Example map

library(tmap)
(tm = tm_shape(World) + tm_polygons("press") + tm_crs("auto"))

Save with tmap_save()

tmap_save(tm, filename = "press_freedom.png", width = 2000, height = 800)
  • height and width can be set in pixels or inches
  • dpi is used to convert them
  • Set the width and height for an optimal layout

Printing devices

  • Under the hood, tmap makes use of the core functions from grDevices, like png() and pdf()
  • Options from those functions can be passed on, e.g.
    • type rendering output type, "cairo-png" is the default for png.
    • colormodel "srgb" is the default for, but for high print quality, a "cmyk" (cyan, magenta, yellow, black) is often required.

Scaling

Use scale to increase or decrease scalable viusal elements, like font size and line width

tmap_save(tm, filename = "press_freedom_scale_0_5.png", width = 2000, height = 800, scale = 0.5)
tmap_save(tm, filename = "press_freedom_scale_2_0.png", width = 2000, height = 800, scale = 2)

Saving interactive plots

Save with tmap_save()

Use the .html extension to save a standalone interactive HTML file — the map can be opened in any browser, no R needed.

tmap_save(tm, filename = "press_freedom.html")
  • If the active mode is "view", "maplibre", or "mapbox", the interactive map is saved
  • If the active mode is "plot", tmap switches to "view" mode automatically for the HTML export

HTML files

  • in.iframe places the map in an iframe, which is a html container.
  • selfcontained (default TRUE) determines if dependencies (JS, CSS etc) are included in the file
tmap_save(tm, filename = "press_freedom.html", in.iframe = TRUE)
tmap_save(tm, filename = "press_freedom.html", selfcontained = FALSE)

Saving animations

Animations with tm_animate()

Animations work like facets — replace tm_facets() with tm_animate() to get frames instead of panels. By default the animation plays in the RStudio Viewer pane:

m <- tm_shape(World) +
  tm_fill("#dddddd") +
tm_shape(metro) +
  tm_bubbles(
    size = paste0("pop", seq(1960, 2030, by = 10)),
    size.scale = tm_scale_continuous(values.scale = 2),
    size.legend = tm_legend("Population"),
    fill = "gold",
    size.free = FALSE) +
tm_animate(fps = 4) +
tm_crs("auto")

print(m)

Saving animations with tmap_animation()

Save to GIF or MP4 with tmap_animation():

tmap_animation(m, filename = "metro_growth.gif")
tmap_animation(m, filename = "metro_growth.mp4", width = 1200, height = 600, dpi = 150)

fps is set in tm_animate(); tmap_animation() controls the file format and frame size/resolution (width, height, dpi).

More: adv_animations vignette

Recap

  • tmap_save(tm, "map.png") — save a static map (PNG, PDF, SVG, …)
  • tmap_save(tm, "map.html") — save a standalone interactive HTML file
    • Interactive modes ("view", "maplibre", "mapbox") save as-is; "plot" mode switches to "view" automatically
  • selfcontained = TRUE (default) — all JS/CSS bundled in the HTML file
  • tm_animate(fps = 4) — creates an animation (frames instead of facet panels); fps set here
  • tmap_animation(m, "anim.gif") — exports to GIF or MP4; controls frame size/resolution (width, height, dpi)