tm_symbols | R Documentation |
Creates a tmap-element
that draws symbols, including symbols and dots. The color, size, and shape of the symbols can be mapped to data variables.
tm_symbols(
size = 1,
col = NA,
shape = 21,
alpha = NA,
border.col = NA,
border.lwd = 1,
border.alpha = NA,
scale = 1,
perceptual = FALSE,
clustering = FALSE,
size.max = NA,
size.lim = NA,
sizes.legend = NULL,
sizes.legend.labels = NULL,
n = 5,
style = ifelse(is.null(breaks), "pretty", "fixed"),
style.args = list(),
as.count = NA,
breaks = NULL,
interval.closure = "left",
palette = NULL,
labels = NULL,
drop.levels = FALSE,
midpoint = NULL,
stretch.palette = TRUE,
contrast = NA,
colorNA = NA,
textNA = "Missing",
showNA = NA,
colorNULL = NA,
shapes = 21:25,
shapes.legend = NULL,
shapes.legend.fill = NA,
shapes.labels = NULL,
shapes.drop.levels = FALSE,
shapeNA = 4,
shape.textNA = "Missing",
shape.showNA = NA,
shapes.n = 5,
shapes.style = ifelse(is.null(shapes.breaks), "pretty", "fixed"),
shapes.style.args = list(),
shapes.as.count = NA,
shapes.breaks = NULL,
shapes.interval.closure = "left",
legend.max.symbol.size = 0.8,
just = NA,
jitter = 0,
xmod = 0,
ymod = 0,
icon.scale = 3,
grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256),
title.size = NA,
title.col = NA,
title.shape = NA,
legend.size.show = TRUE,
legend.col.show = TRUE,
legend.shape.show = TRUE,
legend.format = list(),
legend.size.is.portrait = FALSE,
legend.col.is.portrait = TRUE,
legend.shape.is.portrait = TRUE,
legend.size.reverse = FALSE,
legend.col.reverse = FALSE,
legend.shape.reverse = FALSE,
legend.hist = FALSE,
legend.hist.title = NA,
legend.size.z = NA,
legend.col.z = NA,
legend.shape.z = NA,
legend.hist.z = NA,
id = NA,
interactive = TRUE,
popup.vars = NA,
popup.format = list(),
zindex = NA,
group = NA,
auto.palette.mapping = NULL,
max.categories = NULL
)
tm_squares(size = 1, col = NA, shape = 22, scale = 4/3, ...)
tm_bubbles(
size = 1,
col = NA,
shape = 21,
scale = 4/3,
legend.max.symbol.size = 1,
...
)
tm_dots(
col = NA,
size = 0.02,
shape = 19,
title = NA,
legend.show = TRUE,
legend.is.portrait = TRUE,
legend.z = NA,
...
)
tm_markers(
shape = marker_icon(),
col = NA,
border.col = NULL,
clustering = TRUE,
text = NULL,
text.just = "top",
markers.on.top.of.text = TRUE,
group = NA,
...
)
size |
a single value or a |
col |
color(s) of the symbol. Either a color (vector), or categorical variable name(s). If multiple values are specified, small multiples are drawn (see details). |
shape |
shape(s) of the symbol. Either direct shape specification(s) or a data variable name(s) that is mapped to the symbols specified by the |
alpha |
transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the |
border.col |
color of the symbol borders. |
border.lwd |
line width of the symbol borders. If |
border.alpha |
transparency number, regarding the symbol borders, between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the |
scale |
symbol size multiplier number. |
perceptual |
by default (with |
clustering |
value that determines whether the symbols are clustered in |
size.max |
value that is mapped to |
size.lim |
vector of two limit values of the |
sizes.legend |
vector of symbol sizes that are shown in the legend. By default, this is determined automatically. |
sizes.legend.labels |
vector of labels for that correspond to |
n |
preferred number of color scale classes. Only applicable when |
style |
method to process the color scale when |
style.args |
arguments passed on to |
as.count |
when |
breaks |
in case |
interval.closure |
value that determines whether where the intervals are closed: |
palette |
a palette name or a vector of colors. See |
labels |
labels of the classes |
drop.levels |
should unused classes be omitted? |
midpoint |
The value mapped to the middle color of a diverging palette. By default it is set to 0 if negative and positive values are present. In that case, the two sides of the color palette are assigned to negative respectively positive values. If all values are positive or all values are negative, then the midpoint is set to |
stretch.palette |
Logical that determines whether the categorical color palette should be stretched if there are more categories than colors. If |
contrast |
vector of two numbers that determine the range that is used for sequential and diverging palettes (applicable when |
colorNA |
colour for missing values. Use |
textNA |
text used for missing values of the color variable. |
showNA |
logical that determines whether missing values are named in the legend. By default ( |
colorNULL |
colour for polygons that are shown on the map that are out of scope |
shapes |
palette of symbol shapes. Only applicable if |
shapes.legend |
symbol shapes that are used in the legend (instead of the symbols specified with |
shapes.legend.fill |
Fill color of legend shapes. These colors will be used in the legends regarding the |
shapes.labels |
Legend labels for the symbol shapes |
shapes.drop.levels |
should unused symbol classes be omitted? |
shapeNA |
the shape (a number or grob) for missing values. By default a cross (number 4). Set to |
shape.textNA |
text used for missing values of the shape variable. |
shape.showNA |
logical that determines whether missing values are named in the legend. By default ( |
shapes.n |
preferred number of shape classes. Only applicable when |
shapes.style |
method to process the shape scale when |
shapes.style.args |
arguments passed on to |
shapes.as.count |
when |
shapes.breaks |
in case |
shapes.interval.closure |
value that determines whether where the intervals are closed: |
legend.max.symbol.size |
Maximum size of the symbols that are drawn in the legend. For circles and bubbles, a value larger than one is recommended (and used for |
just |
justification of the symbols relative to the point coordinates. The first value specifies horizontal and the second value vertical justification. Possible values are: |
jitter |
number that determines the amount of jittering, i.e. the random noise added to the position of the symbols. 0 means no jittering is applied, any positive number means that the random noise has a standard deviation of |
xmod |
horizontal position modification of the symbols, in terms of the height of one line of text. Either a single number for all polygons, or a numeric variable in the shape data specifying a number for each polygon. Together with |
ymod |
vertical position modification. See xmod. |
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 |
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. |
title.size |
title of the legend element regarding the symbol sizes |
title.col |
title of the legend element regarding the symbol colors |
title.shape |
title of the legend element regarding the symbol shapes |
legend.size.show |
logical that determines whether the legend for the symbol sizes is shown |
legend.col.show |
logical that determines whether the legend for the symbol colors is shown |
legend.shape.show |
logical that determines whether the legend for the symbol shapes is shown |
legend.format |
list of formatting options for the legend numbers. Only applicable if
|
legend.size.is.portrait |
logical that determines whether the legend element regarding the symbol sizes is in portrait mode ( |
legend.col.is.portrait |
logical that determines whether the legend element regarding the symbol colors is in portrait mode ( |
legend.shape.is.portrait |
logical that determines whether the legend element regarding the symbol shapes is in portrait mode ( |
legend.size.reverse |
logical that determines whether the items of the legend regarding the symbol sizes are shown in reverse order, i.e. from bottom to top when |
legend.col.reverse |
logical that determines whether the items of the legend regarding the symbol colors are shown in reverse order, i.e. from bottom to top when |
legend.shape.reverse |
logical that determines whether the items of the legend regarding the symbol shapes are shown in reverse order, i.e. from bottom to top when |
legend.hist |
logical that determines whether a histogram is shown regarding the symbol colors |
legend.hist.title |
title for the histogram. By default, one title is used for both the histogram and the normal legend for symbol colors. |
legend.size.z |
index value that determines the position of the legend element regarding the symbol sizes with respect to other legend elements. The legend elements are stacked according to their z values. The legend element with the lowest z value is placed on top. |
legend.col.z |
index value that determines the position of the legend element regarding the symbol colors. (See |
legend.shape.z |
index value that determines the position of the legend element regarding the symbol shapes. (See |
legend.hist.z |
index value that determines the position of the histogram legend element. (See |
id |
name of the data variable that specifies the indices of the symbols. Only used for |
interactive |
logical that determines whether this layer is interactive in view mode (e.g. hover text, popup, and click event in shiny apps) |
popup.vars |
names of data variables that are shown in the popups in |
popup.format |
list of formatting options for the popup values. See the argument |
zindex |
zindex of the pane in view mode. By default, it is set to the layer number plus 400. By default, the tmap layers will therefore be placed in the custom panes |
group |
name of the group to which this layer belongs in view mode. Each group can be selected or deselected in the layer control item. Set |
auto.palette.mapping |
deprecated. It has been replaced by |
max.categories |
deprecated. It has moved to |
... |
arguments passed on to |
title |
shortcut for |
legend.show |
shortcut for |
legend.is.portrait |
shortcut for |
legend.z |
shortcut for |
text |
text of the markers. Shown in plot mode, and as popup text in view mode. |
text.just |
justification of marker text (see |
markers.on.top.of.text |
For |
Small multiples can be drawn in two ways: either by specifying the by
argument in tm_facets
, or by defining multiple variables in the aesthetic arguments, which are size
, col
, and shape
. In the latter case, the arguments, except for the ones starting with legend.
, can be specified for small multiples as follows. If the argument normally only takes a single value, such as n
, then a vector of those values can be specified, one for each small multiple. If the argument normally can take a vector, such as palette
, then a list of those vectors (or values) can be specified, one for each small multiple.
A shape specification is one of the following three options.
A numeric value that specifies the plotting character of the symbol. See parameter pch
of points
and the last example to create a plot with all options. Note that this is not supported for the "view" mode.
A grob
object, which can be a ggplot2 plot object created with ggplotGrob
. To specify multiple shapes, a list of grob objects is required. See example of a proportional symbol map with ggplot2 plots.
An icon specification, which can be created with tmap_icons
.
To specify multiple shapes (needed for the shapes
argument), a vector or list of these shape specification is required. The shape specification options can also be mixed. For the shapes
argument, it is possible to use a named vector or list, where the names correspond to the value of the variable specified by the shape
argument.
For small multiples, a list of these shape specification(s) should be provided.
tmap-element
Flannery J (1971). The Relative Effectiveness of Some Common Graduated Point Symbols in the Presentation of Quantitative Data. Canadian Cartographer, 8(2), 96-109.
Tennekes, M., 2018, tmap: Thematic Maps in R, Journal of Statistical Software, 84(6), 1-39, \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v084.i06")}
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_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")
if (require(ggplot2) && require(dplyr) && require(tidyr) && require(tmaptools) && require(sf)) {
data(NLD_prov)
origin_data <- NLD_prov %>%
st_set_geometry(NULL) %>%
mutate(FID= factor(1:n())) %>%
select(FID, origin_native, origin_west, origin_non_west) %>%
gather(key=origin, value=perc, origin_native, origin_west, origin_non_west, factor_key=TRUE)
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")
}
# TIP: check out these examples in view mode, enabled with tmap_mode("view")
## Not run:
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 = NULL, id="name", popup.vars = TRUE) +
tm_view(set.view = c(lon = 15, lat = 48, zoom = 4))
tmap_mode(current.mode)
}
## End(Not run)
#####################################################################################
## Not run:
# 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())
}
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.