tmap (version 1.10)

tmap_mode: Set tmap mode to static plotting or interactive viewing

Description

Set tmap mode to static plotting or interactive viewing. The global option tmap.mode determines the whether thematic maps are plot in the graphics device, or shown as an interactive leaflet map (see also tmap_options. The function tmap_mode is a wrapper to set this global option. The convenient function ttm is a toggle switch between the two modes. Tip 1: use tmap_mode in scripts and ttm in the console. Tip 2: use ttm in combination with last_map to redraw the last map in the other mode.

Usage

tmap_mode(mode = c("plot", "view"))

ttm()

Arguments

mode

one of

"plot"

Thematic maps are shown in the graphics device. This is the default mode, and supports all tmap's features, such as small multiples (see tm_facets) and extensive layout settings (see tm_layout). It is recommended for saving static maps (see save_tmap).

"view"

Thematic maps are viewed interactively in the web browser or RStudio's Viewer pane. Maps are fully interactive with tiles from OpenStreetMap or other map providers. See tm_view for options related to the "view" mode. This mode generates a leaflet widget, which can also be directly obtained with tmap_leaflet. With RMarkdown, it is possible to publish it to an HTML page. There are a couple of contraints in comparison to "plot":

  • The map is always projected accoring to the Web Mercator projection. Although this projection is the de facto standard for interactive web-based mapping, it lacks the equal-area property, which is important for many thematic maps, especially choropleths (see examples from tm_shape).

  • Small multiples are not supported

  • The legend cannot be made for aesthetics regarding size, which are symbol size and line width.

  • Text labels are not supported (yet)

  • The layout options set with tm_layout) regarding map format are not used. However, the styling options still apply.

Value

the mode before changing

See Also

vignette("tmap-modes"), last_map to show the last map, tm_view for viewing options, and tmap_leaflet for obtaining a leaflet widget, and tmap_options for tmap options.

Examples

Run this code
# NOT RUN {
# world choropleth/bubble map of the world
data(World, metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100

map1 <- tm_shape(World) +
	tm_polygons("income_grp", palette="-Blues", contrast=.7, id="name", title="Income group") +
	tm_shape(metro) +
	tm_bubbles("pop2010", col = "growth", 
		border.col = "black", border.alpha = .5, 
		style="fixed", breaks=c(-Inf, seq(0, 6, by=2), Inf),
		palette="-RdYlBu", contrast=1, 
		title.size="Metro population", 
		title.col="Growth rate (%)", id="name",
		popup.vars = c("pop2010", "pop2020", "growth")) + 
	tm_layout(legend.bg.color = "grey90", legend.bg.alpha=.5, legend.frame=TRUE)

# initial mode: "plot"
current.mode <- tmap_mode("plot")

# plot map
map1

# switch to other mode: "view"
ttm()

# view map
map1

# }
# NOT RUN {
# choropleth of the Dutch population in interactive mode:
require(tmaptools)
data(NLD_muni, NLD_prov)
NLD_muni$pop_dens <- calc_densities(NLD_muni, var = "population")

tm_shape(NLD_muni) +
	tm_fill(col="pop_dens", 
		style="kmeans", 
		title = "Population (per km^2)", id = "name") +
	tm_borders("grey25", alpha=.5) + 
	tm_shape(NLD_prov) +
	tm_borders("grey40", lwd=2)
# }
# NOT RUN {
# restore current mode
tmap_mode(current.mode)
# }

Run the code above in your browser using DataCamp Workspace