Map layer that draws symbols Supported visual variables are:
fill
(the fill color), col
(the border color), size
the symbol size,
shape
the symbol shape, lwd
(line width), lty
(line type), fill_alpha
(fill color alpha transparency) and col_alpha
(border color alpha transparency).
Usage
opt_tm_symbols(
points.only = "ifany",
icon.scale = 3,
just = NA,
grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256)
)
opt_tm_dots(
points.only = "ifany",
icon.scale = 3,
just = NA,
grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256)
)
opt_tm_bubbles(
points.only = "ifany",
icon.scale = 3,
just = NA,
grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256)
)
opt_tm_sqaures(
points.only = "ifany",
icon.scale = 3,
just = NA,
grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256)
)
tm_symbols(
size = tm_const(),
size.scale = tm_scale(),
size.legend = tm_legend(),
size.chart = tm_chart_none(),
size.free = NA,
fill = tm_const(),
fill.scale = tm_scale(),
fill.legend = tm_legend(),
fill.chart = tm_chart_none(),
fill.free = NA,
col = tm_const(),
col.scale = tm_scale(),
col.legend = tm_legend(),
col.chart = tm_chart_none(),
col.free = NA,
shape = tm_const(),
shape.scale = tm_scale(),
shape.legend = tm_legend(),
shape.chart = tm_chart_none(),
shape.free = NA,
lwd = tm_const(),
lwd.scale = tm_scale(),
lwd.legend = tm_legend(),
lwd.chart = tm_chart_none(),
lwd.free = NA,
lty = tm_const(),
lty.scale = tm_scale(),
lty.legend = tm_legend(),
lty.chart = tm_chart_none(),
lty.free = NA,
fill_alpha = tm_const(),
fill_alpha.scale = tm_scale(),
fill_alpha.legend = tm_legend(),
fill_alpha.chart = tm_chart_none(),
fill_alpha.free = NA,
col_alpha = tm_const(),
col_alpha.scale = tm_scale(),
col_alpha.legend = tm_legend(),
col_alpha.chart = tm_chart_none(),
col_alpha.free = NA,
plot.order = tm_plot_order("size"),
zindex = NA,
group = NA,
group.control = "check",
popup.vars = NA,
popup.format = list(),
hover = "",
id = "",
options = opt_tm_symbols(),
...
)
tm_dots(
fill = tm_const(),
fill.scale = tm_scale(),
fill.legend = tm_legend(),
fill.free = NA,
size = tm_const(),
size.scale = tm_scale(),
size.legend = tm_legend(),
size.free = NA,
lwd = tm_const(),
lwd.scale = tm_scale(),
lwd.legend = tm_legend(),
lwd.free = NA,
lty = tm_const(),
lty.scale = tm_scale(),
lty.legend = tm_legend(),
lty.free = NA,
fill_alpha = tm_const(),
fill_alpha.scale = tm_scale(),
fill_alpha.legend = tm_legend(),
fill_alpha.free = NA,
plot.order = tm_plot_order("size"),
zindex = NA,
group = NA,
group.control = "check",
options = opt_tm_dots(),
...
)
tm_bubbles(
size = tm_const(),
size.scale = tm_scale(),
size.legend = tm_legend(),
size.free = NA,
fill = tm_const(),
fill.scale = tm_scale(),
fill.legend = tm_legend(),
fill.free = NA,
col = tm_const(),
col.scale = tm_scale(),
col.legend = tm_legend(),
col.free = NA,
lwd = tm_const(),
lwd.scale = tm_scale(),
lwd.legend = tm_legend(),
lwd.free = NA,
lty = tm_const(),
lty.scale = tm_scale(),
lty.legend = tm_legend(),
lty.free = NA,
fill_alpha = tm_const(),
fill_alpha.scale = tm_scale(),
fill_alpha.legend = tm_legend(),
fill_alpha.free = NA,
col_alpha = tm_const(),
col_alpha.scale = tm_scale(),
col_alpha.legend = tm_legend(),
col_alpha.free = NA,
plot.order = tm_plot_order("size"),
zindex = NA,
group = NA,
group.control = "check",
options = opt_tm_bubbles(),
...
)
tm_squares(
size = tm_const(),
size.scale = tm_scale(),
size.legend = tm_legend(),
size.free = NA,
fill = tm_const(),
fill.scale = tm_scale(),
fill.legend = tm_legend(),
fill.free = NA,
col = tm_const(),
col.scale = tm_scale(),
col.legend = tm_legend(),
col.free = NA,
lwd = tm_const(),
lwd.scale = tm_scale(),
lwd.legend = tm_legend(),
lwd.free = NA,
lty = tm_const(),
lty.scale = tm_scale(),
lty.legend = tm_legend(),
lty.free = NA,
fill_alpha = tm_const(),
fill_alpha.scale = tm_scale(),
fill_alpha.legend = tm_legend(),
fill_alpha.free = NA,
col_alpha = tm_const(),
col_alpha.scale = tm_scale(),
col_alpha.legend = tm_legend(),
col_alpha.free = NA,
plot.order = tm_plot_order("size"),
zindex = NA,
group = NA,
group.control = "check",
options = opt_tm_sqaures(),
...
)
Arguments
- points.only
should only point geometries of the shape object (defined in
tm_shape()
) be plotted? By default"ifany"
, which meansTRUE
in case a geometry collection is specified.- icon.scale
scaling number that determines how large the icons (or grobs) are in plot mode in comparison to proportional symbols (such as bubbles). In view mode, the size is determined by the icon specification (see
tmap_icons
) or, if grobs are specified bygrob.width
andgrob.heigth
- just
justification of the text relative to the point coordinates. Either one of the following values:
"left"
,"right"
,"center"
,"bottom"
, and"top"
, or a vector of two values where first value specifies horizontal and the second value vertical justification. Besides the mentioned values, also numeric values between 0 and 1 can be used. 0 means left justification for the first value and bottom justification for the second value. Note that in view mode, only one value is used.- grob.dim
vector of four values that determine how grob objects (see details) are shown in view mode. The first and second value are the width and height of the displayed icon. The third and fourth value are the width and height of the rendered png image that is used for the icon. Generally, the third and fourth value should be large enough to render a ggplot2 graphic successfully. Only needed for the view mode.
- size, size.scale, size.legend, size.chart, size.free
Visual variable that determines the size. See details.
- fill, fill.scale, fill.legend, fill.chart, fill.free
Visual variable that determines the fill color. See details.
- col, col.scale, col.legend, col.chart, col.free
Visual variable that determines the col color. See details.
- shape, shape.scale, shape.legend, shape.chart, shape.free
Visual variable that determines the shape. See details.
- lwd, lwd.scale, lwd.legend, lwd.chart, lwd.free
Visual variable that determines the line width. See details.
- lty, lty.scale, lty.legend, lty.chart, lty.free
Visual variable that determines the line type. See details.
- fill_alpha, fill_alpha.scale, fill_alpha.legend, fill_alpha.chart, fill_alpha.free
Visual variable that determines the fill color alpha transparency See details.
- col_alpha, col_alpha.scale, col_alpha.legend, col_alpha.chart, col_alpha.free
Visual variable that determines the border color alpha transparency. See details.
- plot.order
Specification in which order the spatial features are drawn. See
tm_plot_order()
for details.- zindex
Map layers are drawn on top of each other. The
zindex
numbers (one for each map layer) determines the stacking order. By default the map layers are drawn in the order they are called.- group
Name of the group to which this layer belongs. This is only relevant in view mode, where layer groups can be switched (see
group.control
)- group.control
In view mode, the group control determines how layer groups can be switched on and off. Options:
"radio"
for radio buttons (meaning only one group can be shown),"check"
for check boxes (so multiple groups can be shown), and"none"
for no control (the group cannot be (de)selected).- popup.vars
names of data variables that are shown in the popups in
"view"
mode. Set popup.vars toTRUE
to show all variables in the shape object. Set popup.vars toFALSE
to disable popups. Set popup.vars to a character vector of variable names to those those variables in the popups. The default (NA
) depends on whether visual variables (e.g.col
) are used. If so, only those are shown. If not all variables in the shape object are shown.- popup.format
list of formatting options for the popup values. See the argument
legend.format
for options. Only applicable for numeric data variables. If one list of formatting options is provided, it is applied to all numeric variables ofpopup.vars
. Also, a (named) list of lists can be provided. In that case, each list of formatting options is applied to the named variable.- hover
name of the data variable that specifies the hover labels
- id
name of the data variable that specifies the indices of the spatial features. Only used for
"view"
mode.- options
options passed on to the corresponding
opt_<layer_function>
function- ...
to catch deprecated arguments from version < 4.0
Details
The visual variable arguments (e.g. col
) can be specified with either a data
variable name (e.g., a spatial vector attribute or a raster layer of the object
specified in tm_shape()
), or with a visual value (for col
, a color is expected).
Multiple values can be specified: in that case facets are created.
These facets can be combined with other faceting data variables, specified with tm_facets()
.
The
*.scale
arguments determine the used scale to map the data values to visual variable values. These can be specified with one of the availabletm_scale_*()
functions. The default is specified by the tmap option (tm_options()
)scales.var
.The
*.legend
arguments determine the used legend, specified withtm_legend()
. The default legend and its settings are determined by the tmap options (tm_options()
)legend.
.The
*.chart
arguments specify additional charts, specified withtm_chart_
, e.g.tm_chart_histogram()
The
*.free
arguments determine whether scales are applied freely across facets, or shared. A logical value is required. They can also be specified with a vector of three logical values; these determine whether scales are applied freely per facet dimension. This is only useful when facets are applied (seetm_facets()
). There are maximally three facet dimensions: rows, columns, and pages. This only applies for a facet grid (tm_facets_grid()
). For instance,col.free = c(TRUE, FALSE, FALSE)
means that for the visual variablecol
, each row of facets will have its own scale, and therefore its own legend. For facet wraps and stacks (tm_facets_wrap()
andtm_facets_stack()
) there is only one facet dimension, so the*.free
argument requires only one logical value.
Examples
metroAfrica = sf::st_intersection(metro, World[World$continent == "Africa", ])
#> Warning: attribute variables are assumed to be spatially constant throughout all geometries
Africa = World[World$continent == "Africa", ]
tm_shape(land) +
tm_raster("cover_cls",
col.scale = tm_scale(values = cols4all::c4a("brewer.pastel1")[c(3,7,7,2,6,1,2,2)]),
col.legend = tm_legend_hide()) +
tm_shape(rivers) +
tm_lines(lwd = "strokelwd", lwd.scale = tm_scale_asis(values.scale = .3), col = cols4all::c4a("brewer.pastel1")[2]) +
tm_shape(Africa, is.main = TRUE) +
tm_borders() +
tm_shape(metroAfrica) +
tm_symbols(fill = "red", shape = "pop2020", size = "pop2020",
size.scale = tm_scale_intervals(breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6, values.range = c(0.2,2)),
size.legend = tm_legend("Population in 2020"),
shape.scale = tm_scale_intervals(breaks = c(1, 2, 5, 10, 15, 20, 25) * 1e6, values = c(21, 23, 22, 21, 23, 22)),
shape.legend = tm_legend_combine("size")) +
tm_labels("name", options = opt_tm_labels(remove.overlap = FALSE))
##### tmap v3
data(World, metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
tm_shape(World) +
tm_fill("grey70") +
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 (%)") +
tm_format("World")
#> tm_symbols: Deprecated tmap v3 code detected. Code translated to v4
#> Variable(s) "fill" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
tm_shape(metro) +
tm_symbols(size = "pop2010", col="pop2010", shape="pop2010",
legend.format = list(text.align="right", text.to.columns = TRUE)) +
tm_legend(outside = TRUE, outside.position = "bottom", stack = "horizontal")
#> tm_symbols: Deprecated tmap v3 code detected. Code translated to v4
#> Warning: v3 code detected: as of tmap v4, tm_legend should be specified per visual variable (e.g. with the argument fill.legend of tm_polygons
if (require(ggplot2) && require(dplyr) && require(tidyr) && require(tmaptools) && require(sf)) {
data(NLD_prov)
origin_data <- NLD_prov %>%
st_set_geometry(NULL) %>%
dplyr::mutate(FID = factor(dplyr::row_number())) %>%
dplyr::select(FID, origin_native, origin_west, origin_non_west) %>%
tidyr::pivot_longer(
cols = c(origin_native, origin_west, origin_non_west),
names_to = "origin",
values_to = "perc",
names_transform = as.factor
) %>%
dplyr::arrange(origin, FID) %>%
as.data.frame()
origin_cols <- get_brewer_pal("Dark2", 3)
grobs <- lapply(split(origin_data, origin_data$FID), function(x) {
ggplotGrob(ggplot(x, aes(x="", y=-perc, fill=origin)) +
geom_bar(width=1, stat="identity") +
scale_y_continuous(expand=c(0,0)) +
scale_fill_manual(values=origin_cols) +
theme_ps(plot.axes = FALSE))
})
names(grobs) <- NLD_prov$name
tm_shape(NLD_prov) +
tm_polygons(group = "Provinces") +
tm_symbols(size = "population", shape="name",
shapes = grobs,
sizes.legend = c(.5, 1,3)*1e6,
scale = 1,
legend.shape.show = FALSE,
legend.size.is.portrait = TRUE,
shapes.legend = 22,
title.size = "Population",
group = "Charts",
id = "name",
popup.vars = c("population", "origin_native",
"origin_west", "origin_non_west")) +
tm_add_legend(type = "fill",
group = "Charts",
col = origin_cols,
labels = c("Native", "Western", "Non-western"),
title = "Origin") +
tm_format("NLD")
grobs2 = grobs
grobs2[[6]] = 21
NLD_prov$population[1:5] = 500000
tm_shape(NLD_prov) +
tm_polygons(group = "Provinces") +
tm_symbols(shape="name",
fill = "red",
col = "blue",
size = "population",
size.scale = tm_scale_continuous(values.scale = 4),
shape.scale = tm_scale_categorical(values = grobs2),
shape.legend = tm_legend_hide())
}
#> Loading required package: dplyr
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
#> Loading required package: tidyr
#> Loading required package: tmaptools
#> Loading required package: sf
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
#> tm_symbols: Deprecated tmap v3 code detected. Code translated to v4
#> Error in grid.Call.graphics(C_setviewport, vp, TRUE): non-finite location and/or size for viewport
# TIP: check out these examples in view mode, enabled with tmap_mode("view")
if (FALSE) {
if (require(rnaturalearth)) {
airports <- ne_download(scale=10, type="airports", returnclass = "sf")
airplane <- tmap_icons(system.file("img/airplane.png", package = "tmap"))
current.mode <- tmap_mode("view")
tm_shape(airports) +
tm_symbols(shape=airplane, size="natlscale",
legend.size.show = FALSE, scale=1,
border.col = NA, id="name", popup.vars = TRUE)
#+ tm_view(set.view = c(lon = 15, lat = 48, zoom = 4))
tmap_mode(current.mode)
}
}
#####################################################################################
if (FALSE) {
# plot all available symbol shapes:
if (require(ggplot2)) {
ggplot(data.frame(p=c(0:25,32:127))) +
geom_point(aes(x=p%%16, y=-(p%/%16), shape=p), size=5, fill="red") +
geom_text(mapping=aes(x=p%%16, y=-(p%/%16+0.25), label=p), size=3) +
scale_shape_identity() +
theme(axis.title=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
panel.background=element_blank())
}
}
tm_shape(metro) +
tm_symbols("pop2010", size.scale = tm_scale_continuous(n = 8, values.scale = 3), shape = 22)
data("NLD_prov")
NLD_prov$x = seq(10, by = 5, length.out = 12)
tm_shape(NLD_prov) +
tm_symbols(size = "x", size.scale = tm_scale_continuous(values.scale = 4)) +
tm_text(text = "x")
#> Error: Incorrect values for layer text, aesthetic text; values should conform visual variable "text".
tm_shape(metro) +
tm_symbols("pop2010", fill = "pop2020",
fill.scale = tm_scale_continuous(),
size.scale = tm_scale_continuous(), shape = 22)