knitr::opts_chunk$set(
  collapse = TRUE,
  fig.width = 8,
  comment = "#>"
)
library(tmap)

Mapview

Mapview is an excellent R package for interactive maps. Although the packages have a lot in common, the focus is different:

Modes / platforms

tmap "view" (with tm_view(use_WebGL = FALSE)) is similar to mapview "leaflet" tmap "view" (with tm_view(use_WebGL = TRUE)) is similar to mapview "leafgl"

tmap does not offer a mode using Mapbox yet.

Default maps

This is the default output of mapview:

library(mapview)
mapview(World)

This is the default output of tmap:

tmap_mode("view")
qtm(World) # qtm stands for 'quick thematic map'

Choropleth

mapview(World, zcol = "HPI")
tm_shape(World) +
  tm_polygons(fill = "HPI")

Mimicking mapview layout

We can use tmap to match the style of mapview:

tm_shape(World) +
  tm_polygons(
    fill = "HPI",
    fill_alpha = 0.6,
    col_alpha = 0.9,
    fill.legend = tm_legend(
      title = "World - HPI",
      position = c("right", "top"), 
      fill_alpha = 1),
    fill.scale = tm_scale_continuous(values = "viridis", n = 7, value.na = "#BEBEBE")
  ) + 
tm_basemap(c("CartoDB.Positron", "CartoDB.DarkMatter", 
  "OpenStreetMap", "Esri.WorldImagery", "OpenTopoMap"))


r-tmap/tmap documentation built on Feb. 28, 2025, 7:54 a.m.