Learn R Programming

tmap (version 1.11)

tm_symbols: Draw symbols

Description

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.

Usage

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"), breaks = NULL,
  interval.closure = "left", palette = NULL, labels = NULL,
  auto.palette.mapping = TRUE, contrast = NA, max.categories = 12,
  colorNA = NA, textNA = "Missing", showNA = NA, shapes = 21:25,
  shapes.legend = NULL, shapes.legend.fill = NA, shapes.labels = NULL,
  shapeNA = 4, shape.textNA = "Missing", shapes.n = 5,
  shapes.style = ifelse(is.null(shapes.breaks), "pretty", "fixed"),
  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,
  popup.vars = NA, popup.format = list())

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 = 16, 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 = c("center", "top"), markers.on.top.of.text = TRUE, ...)

Arguments

size

a single value or a shp data variable that determines the symbol sizes. The reference value size=1 corresponds to the area of symbols that have the same height as one line of text. If a data variable is provided, the symbol sizes are scaled proportionally (or perceptually, see perceptual) where by default the symbol with the largest data value will get size=1 (see also size.max). If multiple values are specified, small multiples are drawn (see details).

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 shapes argument. See details for the shape specification.

alpha

transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the col is used (normally 1).

border.col

color of the symbol borders.

border.lwd

line width of the symbol borders. If NA, no symbol borders are drawn.

border.alpha

transparency number, regarding the symbol borders, between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the col is used (normally 1).

scale

symbol size multiplier number.

perceptual

logical that determines whether symbols are scales with a perceptually (TRUE) or mathematically (FALSE, default value). The perceived area of larger symbols is often underestimated. Flannery (1971) experimentally derived a method to compensate this for symbols, which is enabled by this argument.

clustering

value that determines whether the symbols are clustered in "view" mode. It does not work proportional bubbles (i.e. tm_bubbles). One of: TRUE, FALSE, or the output of markerClusterOptions.

size.max

value that is mapped to size=1. By default (NA), the maximum data value is chosen. Only applicable when size is the name of a numeric variable of shp

size.lim

vector of two limit values of the size variable. Only symbols are drawn whose value is greater than or equal to the first value. Symbols whose values exceed the second value are drawn at the size of the second value. Only applicable when size is the name of a numeric variable of shp

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 sizes.legend.

n

preferred number of color scale classes. Only applicable when col is a numeric variable name.

style

method to process the color scale when col is a numeric variable. Discrete options are "cat", "fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", and "jenks". A numeric variable is processed as a categorial variable when using "cat", i.e. each unique value will correspond to a distinct category. For the other discrete options, see the details in classIntervals. Continuous options are "cont" and "order". The former maps the values of col to a smooth gradient, whereas the latter maps the order of values of col to a smooth gradient. They are the continuous variants of respectively the discrete methods "equal" and quantile".

breaks

in case style=="fixed", breaks should be specified. The breaks argument can also be used when style="cont". In that case, the breaks are mapped evenly to the sequential or divering color palette.

interval.closure

value that determines whether where the intervals are closed: "left" or "right". Only applicable if col is a numerc variable.

palette

a palette name or a vector of colors. See tmaptools::palette_explorer() for the named palettes. Use a "-" as prefix to reverse the palette. The default palette is taken from tm_layout's argument aes.palette, which typically depends on the style. The type of palette from aes.palette is automatically determined, but can be overwritten: use "seq" for sequential, "div" for diverging, and "cat" for categorical.

labels

labels of the classes

auto.palette.mapping

When diverging colour palettes are used (i.e. "RdBu") this method automatically maps colors to values such that the middle colors (mostly white or yellow) are assigned to values of 0, and the two sides of the color palette are assigned to negative respectively positive values. When categorical color palettes are used, this method stretches the palette if there are more levels than colors.

contrast

vector of two numbers that determine the range that is used for sequential and diverging palettes (applicable when auto.palette.mapping=TRUE). Both numbers should be between 0 and 1. The first number determines where the palette begins, and the second number where it ends. For sequential palettes, 0 means the brightest color, and 1 the darkest color. For diverging palettes, 0 means the middle color, and 1 both extremes. If only one number is provided, this number is interpreted as the endpoint (with 0 taken as the start).

max.categories

in case col is the name of a categorical variable, this value determines how many categories (levels) it can have maximally. If the number of levels is higher than max.categories and auto.palette.mapping is FALSE, then levels are combined.

colorNA

colour for missing values. Use NULL for transparency.

textNA

text used for missing values of the color variable.

showNA

logical that determines whether missing values are named in the legend. By default (NA), this depends on the presence of missing values.

shapes

palette of symbol shapes. Only applicable if shape is a (vector of) categorical variable(s). See details for the shape specification. By default, the filled symbols 21 to 25 are taken.

shapes.legend

symbol shapes that are used in the legend (instead of the symbols specified with shape. Especially useful when shapes consist of grobs that have to be represented by neutrally colored shapes (see also shapes.legend.fill.

shapes.legend.fill

Fill color of legend shapes (see shapes.legend)

shapes.labels

Legend labels for the symbol shapes

shapeNA

the shape (a number or grob) for missing values. By default a cross (number 4).

shape.textNA

text used for missing values of the shape variable.

shapes.n

preferred number of shape classes. Only applicable when shape is a numeric variable name.

shapes.style

method to process the shape scale when shape is a numeric variable. See style argument for options

shapes.breaks

in case shapes.style=="fixed", breaks should be specified

shapes.interval.closure

value that determines whether where the intervals are closed: "left" or "right". Only applicable if shape is a numerc variable.

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 tm_bubbles)

just

justification of the symbols relative to the point coordinates. The first value specifies horizontal and the second value vertical justification. Possible values are: "left" , "right", "center", "bottom", and "top". Numeric values of 0 specify left alignment and 1 right alignment. The default value is c("center", "center"). For icons, this value may already be speficied (see tmap_icons). The just, if specified, will overrides this.

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 jitter times the height of one line of text line.

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, it determines position modification of the symbols. See also jitter for random position modifications. In most coordinate systems (projections), the origin is located at the bottom left, so negative xmod move the symbols to the left, and negative ymod values to the bottom.

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 tmap_icons) or, if grobs are specified by grob.width and grob.heigth

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 succesfully. 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 labels is undefined. Parameters are:

fun

Function to specify the labels. It should take a numeric vector, and should return a character vector of the same size. By default it is not specified. If specified, the list items scientific, format, and digits (see below) are not used.

scientific

Should the labels be formatted scientically? If so, square brackets are used, and the format of the numbers is "g". Otherwise, format="f", and text.separator, text.less.than, and text.or.more are used. Also, the numbers are automatically rounded to millions or billions if applicable.

format

By default, "f", i.e. the standard notation xxx.xxx, is used. If scientific=TRUE then "g", which means that numbers are formatted scientically, i.e. n.dddE+nn if needed to save space.

digits

Number of digits after the decimal point if format="f", and the number of significant digits otherwise.

text.separator

Character string to use to separate numbers in the legend (default: "to").

text.less.than

Character value(s) to use to translate "Less than". When a character vector of length 2 is specified, one for each word, these words are aligned when text.to.columns = TRUE

text.or.more

Character value(s) to use to translate "or more". When a character vector of length 2 is specified, one for each word, these words are aligned when text.to.columns = TRUE

text.align

Value that determines how the numbers are aligned, "left", "center" or "right"

. By default "left" for legends in portrait format (legend.is.protrait = TRUE), and "center" otherwise.
text.to.columns

Logical that determines whether the text is aligned to three columns (from, text.separator, to). By default FALSE.

...

Other arguments passed on to formatC

legend.size.is.portrait

logical that determines whether the legend element regarding the symbol sizes is in portrait mode (TRUE) or landscape (FALSE)

legend.col.is.portrait

logical that determines whether the legend element regarding the symbol colors is in portrait mode (TRUE) or landscape (FALSE)

legend.shape.is.portrait

logical that determines whether the legend element regarding the symbol shapes is in portrait mode (TRUE) or landscape (FALSE)

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.size.is.portrait = TRUE and from right to left when legend.size.is.portrait = FALSE

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.col.is.portrait = TRUE and from right to left when legend.col.is.portrait = FALSE

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.shape.is.portrait = TRUE and from right to left when legend.shape.is.portrait = FALSE

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.size.z)

legend.shape.z

index value that determines the position of the legend element regarding the symbol shapes. (See legend.size.z)

legend.hist.z

index value that determines the position of the histogram legend element. (See legend.size.z)

id

name of the data variable that specifies the indices of the symbols. Only used for "view" mode (see tmap_mode).

popup.vars

names of data variables that are shown in the popups in "view" mode. If NA (default), only aesthetic variables (i.e. specified by col and lwd) are shown). If they are not specified, all variables are shown. Set popup.vars to FALSE to disable popups. When a vector of variable names is provided, the names (if specified) are printed in the popups.

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 of popup.vars. Also, a (named) list of lists can be provided. In that case, each list of formatting options is applied to the named variable.

...

arguments passed on to tm_symbols. For tm_markers, arguments can also be passed on to tm_text. In that case, they have to be prefixed with text., e.g. the col argument should be names text.col

title

shortcut for title.col for tm_dots

legend.show

shortcut for legend.col.show for tm_dots

legend.is.portrait

shortcut for legend.col.is.portrait for tm_dots

legend.z

shortcut for legend.col.z shortcut for tm_dots

text

text of the markers. Shown in plot mode, and as popup text in view mode.

text.just

justification of marker text (see just argument of tm_text). Only applicable in plot mode.

markers.on.top.of.text

For tm_markers, should the markers be drawn on top of the text labels?

Value

tmap-element

Details

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. 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 speficied by the shape argument.

  1. 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.

  2. 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.

  3. An icon specification, which can be created with tmap_icons.

For small multiples, a list of these shape specification(s) should be provided.

References

Flannery J (1971). The Relative Effectiveness of Some Common Graduated Point Symbols in the Presentation of Quantitative Data. Canadian Cartographer, 8 (2), 96-109.

See Also

vignette("tmap-nutshell")

Examples

Run this code
# NOT RUN {
data(World, Europe, 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_layout(legend.outside = TRUE, legend.outside.position = "bottom", legend.stack = "horizontal")

# }
# NOT RUN {
require(tmaptools)
x <- sample_dots(World, vars="gdp_md_est", convert2density = TRUE, w = 100000)
tm_shape(x) + 
	tm_dots() + 
tm_layout("World GDP (one dot is 100 billon dollars)", title.position = c("right", "bottom"))
# }
# NOT RUN {
qtm(Europe, bbox="Italy") +
tm_shape(metro) +
	tm_markers(text="name")


if (require(ggplot2) && require(dplyr) && require(tidyr) && require(tmaptools)) {
data(NLD_prov)

origin_data <- NLD_prov@data %>% 
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() +
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",
	id = "name",
	popup.vars = c("population", "origin_native",
				   "origin_west", "origin_non_west")) +
tm_add_legend(type="fill", 
	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")
airplane <- tmap_icons(paste0("http://cdn.mysitemyway.com/etc-mysitemyway/icons/",
	"legacy-previews/icons-256/retro-green-floral-icons-transport-travel/",
	"040553-retro-green-floral-icon-transport-travel-transportation-airplane22.png"))

tmap_mode("view")
tm_shape(airports, bbox="Germany") +
	tm_symbols(shape=airplane, size="natlscale",
		legend.size.show = FALSE, scale=2, border.col = NULL, id="name", popup.vars = TRUE)
}
# }
# 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())
}
# }

Run the code above in your browser using DataLab