Set the options for the interactive tmap viewer. Some of these options can also be set with tm_layout
, since they are style dependent (e.g., the choice of basemaps). The function tm_view
overrides these options when specified.
Usage
tm_view(
alpha,
colorNA,
projection,
symbol.size.fixed,
dot.size.fixed,
text.size.variable,
bbox,
set.bounds,
set.view,
set.zoom.limits,
view.legend.position,
control.position,
legend.position,
leaflet.options
)
Arguments
- alpha
transparency (opacity) parameter applied to whole map. By default, it is set to
0.7
if basemaps are used, and1
otherwise.- colorNA
default color for missing values in interactive mode. If the color of missing values is not defined in the layer functions (e.g.
tm_fill
), then the default color is taken from thena
value of theaes.color
argument intm_layout
. ThiscolorNA
argument (if notNA
itself) overrides that default value. For interactive maps, it can be useful to setcolorNA
toNULL
, which means transparent.- projection
projection. Either a EPSG number, or a
leaflet_crs
object created withleafletCRS
. By default, the Web Mercator (3857) is used, since the vast majority of basemaps are rendered accordingly. Other standards are EPSG numbers 4326 (WGS84) and 3395 (Mercator). If set to 0, the projection of the master shape is used (seetm_shape
) provided that a EPSG number can be extracted.- symbol.size.fixed
should symbol sizes be fixed while zooming?
- dot.size.fixed
should dot sizes be fixed while zooming?
- text.size.variable
should text size variables be allowed in view mode? By default
FALSE
, since in many applications, the main reason to vary text size is to prevent occlusion in plot mode, which is often not a problem in view mode due to the ability to zoom in.- bbox
bounding box. One of the following:
A bounding box (an
sf
bbox object, seest_bbox
, or object that can be read bybb
.Open Street Map search query. The bounding is automatically generated by querying
q
from Open Street Map Nominatim. See https://wiki.openstreetmap.org/wiki/Nominatim.
If set, it overrides
set.view
and all bbox arguments oftm_shape
.- set.bounds
logical that determines whether maximum bounds are set, or a numeric vector of four values that specify the lng1, lat1, lng2, and lat2 coordinates (see
setMaxBounds
).- set.view
numeric vector that determines the view. Either a vector of three: lng, lat, and zoom, or a single value: zoom. See
setView
. Only applicable ifbbox
is not specified- set.zoom.limits
numeric vector of two that set the minimum and maximum zoom levels (see
tileOptions
).- view.legend.position
Character vector of two values, specifying the position of the legend. Use "left" or "right" for the first value and "top" or "bottom" for the second value. It overrides the value of
legend.position
oftm_layout
, unless set toNA
.- control.position
Character vector of two values, specifying the position of the layer control UI. Use "left" or "right" for the first value and "top" or "bottom" for the second value.
- legend.position
not used anymore, renamed to view.legend.position
- leaflet.options
other options passed on via
leafletOptions
to leaflet.js map creation (see leaflet, follow Docs, Map, Creation). Named list, where the names correspond to the variable names. Tip: usezoomSnap
andzoomDelta
for fractional zooming.
References
Tennekes, M., 2018, tmap: Thematic Maps in R, Journal of Statistical Software, 84(6), 1-39, doi:10.18637/jss.v084.i06
Examples
# world choropleth/bubble map of the world
data(World, metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
map1 <- 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_legend(outside=TRUE)
current.mode <- tmap_mode("plot")
#> tmap mode set to plotting
# plot map
map1
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Variable(s) "growth" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
# view map with default view options
tmap_mode("view")
#> tmap mode set to interactive viewing
map1
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Variable(s) "growth" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
#> Legend for symbol sizes not available in view mode.
# view map with changed view options
map1 + tm_view(set.view = c(7, 51, 4)) # longitude 7, latitude 51, zoom 4
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Variable(s) "growth" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
#> Legend for symbol sizes not available in view mode.
# interactive world map in original CRS
tm_shape(World) +tm_polygons("HPI") + tm_view(projection = 0) + tm_basemap(NULL)
# restore current mode
tmap_mode(current.mode)
#> tmap mode set to plotting