Learn R Programming

tmap (version 3.3-3)

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, which stands for toggle thematic map, is a toggle switch between the two modes. The function ttmp stands for toggle thematic map and print last map: it does the same as ttm followed by tmap_last; in order words, it shows the last map in the other mode. It is recommended to use tmap_mode in scripts and ttm/ttmp in the console.

Usage

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

ttm()

ttmp()

Value

the mode before changing

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 tmap_save).

"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_tiles). See also 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 constraints in comparison to "plot":

  • The map is always projected according 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.

References

Tennekes, M., 2018, tmap: Thematic Maps in R, Journal of Statistical Software, 84(6), 1-39, tools:::Rd_expr_doi("10.18637/jss.v084.i06")

See Also

vignette("tmap-getstarted"), tmap_last 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
# 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

if (FALSE) {
# 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)
}

# restore current mode
tmap_mode(current.mode)

Run the code above in your browser using DataLab