knitr::opts_chunk$set(
  collapse = TRUE,
  fig.width = 8,
  comment = "#>"
)
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
   lines <- options$output.lines
   if (is.null(lines)) {
     return(hook_output(x, options))  # pass to default hook
   }
   x <- unlist(strsplit(x, "\n"))
   more <- "..."
   if (length(lines)==1) {        # first n lines
     if (length(x) > lines) {
       # truncate the output, but add ....
       x <- c(head(x, lines), more)
     }
   } else {
     x <- c(more, x[lines], more)
   }
   # paste these lines together
   x <- paste(c(x, ""), collapse = "\n")
   hook_output(x, options)
 })
library(tmap)
tmap_options(scale = 0.75)

Extension types

There are three types of tmap extensions:

  1. New map layer types
  2. New spatial data classes
  3. New output modes

Rather than explaining how to extend tmap for each of these three types (which is rather complex), it is easier to demonstrate with proof-of-concept extension packages:

New map layer types

This type of extension requires:

See tmap.glyphs. A layer function tm_donuts is added. More glyph types layer functions will follow, e.g. tm_pies, or tm_radars.

library(tmap.glyphs)
ZH_muni = NLD_muni[NLD_muni$province == "Zuid-Holland", ]
ZH_muni$income_middle = 100 - ZH_muni$income_high - ZH_muni$income_low

tm_shape(ZH_muni) +
  tm_polygons() +
  tm_donuts(
    parts = tm_vars(c("income_low", "income_middle", "income_high"), multivariate = TRUE),
    fill.scale = tm_scale_categorical(values = "-pu_gn_div"),             
    size = "population",
    size.scale = tm_scale_continuous(ticks = c(50000, 100000, 250000, 500000))) 

New spatial data classes

This type of extension requires methods to obtain:

See tmap.networks which supports sfnetworks

library(sfnetworks)
library(tmap.networks)

(sfn = as_sfnetwork(roxel))

Besides this new spatial data class "sfnetwork", this package also features new map layers, albeit very basic so far:

tm_shape(sfn) +
    tm_network()
tm_shape(sfn) +
    tm_edges(col = "type", lwd = 4) +
    tm_nodes()

New mode

This type of extension is the most difficult one. It requires:

A package in development tmap.mapgl contains two new modes, "mapbox" and "maplibre".

The shiny integration may not work yet.

library(tmap.mapgl)
tmap_mode("maplibre")

tm_shape(NLD_dist) +
  tm_polygons(
    fill = "employment_rate", 
    fill.scale = tm_scale_intervals(values = "scico.roma"),
    lwd = 0.1) +
tm_shape(NLD_muni) +
  tm_polygons(fill = NULL, lwd = 1) +
tm_maplibre(pitch = 75)


r-tmap/tmap documentation built on June 1, 2025, 1:56 p.m.