This element specifies the map layout. The main function tm_layout
controls title, margins, aspect ratio, colors, frame, legend, among many other things. The function tm_legend
is a shortcut to access all legend.
arguments without this prefix. The other functions are wrappers for two purposes: tm_format
specifies position related layout settings such as margins, and tm_style
specifies general styling related layout settings such as colors and font. Typically, the former functions are shape dependent, and the latter functions are shape independent. See details for predefined styles and formats. With tmap.style
, a default style can be specified. Multiple tm_layout
elements (or wrapper functions) can be stacked: called arguments will be overwritten.
tm_layout(
title,
scale,
title.size,
bg.color,
aes.color,
aes.palette,
attr.color,
sepia.intensity,
saturation,
frame,
frame.lwd,
frame.double.line,
asp,
outer.margins,
inner.margins,
between.margin,
outer.bg.color,
fontface,
fontfamily,
compass.type,
earth.boundary,
earth.boundary.color,
earth.boundary.lwd,
earth.datum,
space.color,
legend.show,
legend.only,
legend.outside,
legend.outside.position,
legend.outside.size,
legend.position,
legend.stack,
legend.just,
legend.width,
legend.height,
legend.hist.height,
legend.hist.width,
legend.title.color,
legend.title.size,
legend.title.fontface,
legend.title.fontfamily,
legend.text.color,
legend.text.size,
legend.text.fontface,
legend.text.fontfamily,
legend.hist.size,
legend.format,
legend.frame,
legend.frame.lwd,
legend.bg.color,
legend.bg.alpha,
legend.hist.bg.color,
legend.hist.bg.alpha,
title.snap.to.legend,
title.position,
title.color,
title.fontface,
title.fontfamily,
title.bg.color,
title.bg.alpha,
panel.show,
panel.labels,
panel.label.size,
panel.label.color,
panel.label.fontface,
panel.label.fontfamily,
panel.label.bg.color,
panel.label.height,
panel.label.rot,
main.title,
main.title.size,
main.title.color,
main.title.fontface,
main.title.fontfamily,
main.title.position,
attr.outside,
attr.outside.position,
attr.outside.size,
attr.position,
attr.just,
design.mode
)tm_legend(...)
tm_style(style, ...)
tm_format(format, ...)
Global title of the map. For small multiples, multiple titles can be specified. The title is drawn inside the map. Alternatively, use panel.labels
to print the map as a panel, with the title inside the panel header (especially useful for small multiples). Another alternative is the main.title
which prints a title above the map. Titles for the legend items are specified at the layer functions (e.g. tm_fill
).
numeric value that serves as the global scale parameter. All font sizes, symbol sizes, border widths, and line widths are controlled by this value. Each of these elements can be scaled independently with the scale
, lwd
, or size
arguments provided by the tmap-elements
.
Relative size of the title
Background color. By default it is "white"
. A recommended alternative for choropleths is light grey (e.g., "grey85"
).
Default color values for the aesthetics layers. Should be a named vector with the names chosen from: fill
, borders
, symbols
, dots
, lines
, text
, na
. Use "#00000000"
for transparency.
Default color palettes for the aesthetics. It takes a list of three items: seq
for sequential palettes, div
for diverging palettes, and cat
for categorical palettes. By default, Color Brewer palettes (see (see tmaptools::palette_explorer()
)) are used. It is also possible provide a vector of colors for any of these items.
Default color value for map attributes
Number between 0 and 1 that defines the amount of sepia effect, which gives the map a brown/yellowish flavour. By default this effect is disabled (sepia.intensity=0
). All colored used in the map are adjusted with this effect.
Number that determines how much saturation (also known as chroma) is used: saturation=0
is greyscale and saturation=1
is normal. A number larger than 1 results in very saturated maps. All colored used in the map are adjusted with this effect. Hacking tip: use a negative number.
Either a boolean that determines whether a frame is drawn, or a color value that specifies the color of the frame.
width of the frame
draw a double frame line border?
Aspect ratio. The aspect ratio of the map (width/height). If NA
, it is determined by the bounding box (see argument bbox
of tm_shape
), the outer.margins
, and the inner.margins
. If 0
, then the aspect ratio is adjusted to the aspect ratio of the device.
Relative margins between device and frame. Vector of four values specifying the bottom, left, top, and right margin. Values are between 0 and 1. When facets are created, the outer margins are the margins between the outer panels and the device borders (see also between.margin
)
Relative margins inside the frame. Vector of four values specifying the bottom, left, top, and right margin. Values are between 0 and 1. By default, 0 for each side if master shape is a raster, otherwise 0.02.
Margin between facets (small multiples) in number of text line heights. The height of a text line is automatically scaled down based on the number of facets.
Background color outside the frame.
global font face for the text in the map. It can also be set locally per element (see e.g. title.fontface
).
global font family for the text in the map. It can also be set locally per (see e.g. title.fontfamily
).
type of compass, one of: "arrow"
, "4star"
, "8star"
, "radar"
, "rose"
. Of course, only applicable if a compass is shown. The compass type can also be set within tm_compass
.
Logical that determines whether the boundaries of the earth are shown or an object that specifies the boundaries. This object can be a vector of size four, a 2 by 2 matrix (bounding box), or an extent
object. By default, the boundaries are c(-180, -90, 180, 90)
. Useful for projected world maps. Often, it is useful to crop both poles (e.g., with c(-180, -88, 180, 88)
).
Color of the earth boundary.
Line width of the earth boundary.
Geodetic datum to determine the earth boundary. By default epsg 4326
(long/lat).
Color of the space, i.e. the region inside the frame, and outside the earth boundary.
Logical that determines whether the legend is shown.
logical. Only draw the legend (without map)? Particularly useful for small multiples with a common legend.
Logical that determines whether the legend is plot outside of the map/facets. Especially useful when using facets that have a common legend (i.e. with free.scales=FALSE
).
Character that determines the outside position of the legend. Only applicable when legend.outside=TRUE
. One of: "right"
, "left"
, "top"
, or "bottom"
.
Numeric value that determines the relative size of the legend, when legend.outside=TRUE
. If the first value of legend.outside.position
is "top"
or "bottom"
, then it is the width of the legend, else it is the height of the legend. Note that the actual height or width of the legend is determined by the content of the legend (and the used font sizes). This argument specifies the upperbound of the width or height.
Position of the legend. Vector of two values, specifying the x and y coordinates. Either this vector contains "left"
, "LEFT"
, "center"
, "right"
, or "RIGHT"
for the first value and "top"
, "TOP"
, "center"
, "bottom"
, or "BOTTOM"
for the second value, or this vector contains two numeric values between 0 and 1 that specifies the x and y coordinates of the left bottom corner of the legend. The uppercase values correspond to the position without margins (so tighter to the frame). By default, it is automatically placed in the corner with most space based on the (first) shape object. If legend.outside=TRUE
, this argument specifies the legend position within the outside panel.
Value that determines how different legends are stacked: "vertical"
or "horizontal"
. To stack items within a same legend, look at "legend.is.portrait"
in the specific layer calls.
Justification of the legend 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/bottom alignment and 1 right/top alignment. This option is only used, if legend.position
is specified by numeric coordinates.
width of the legend. This number is relative to the map area (so 1 means the whole map width). If it is a negative number, it will be the exact legend width. If it is a positive number (by default), it will be the maximum legend width; the actual legend width will be decreased automatically based on the legend content and font sizes.or Default color value for map attributes
height of the legend. If it is a negative number, it will be the exact legend height. If it is a positive number (by default), it will be the maximum legend height; the actual legend height will be decreased automatically based on the legend content and font sizes.
height of the histogram. This height is initial. If the total legend is downscaled to legend.height
, the histogram is downscaled as well.
width of the histogram. By default, it is equal to the legend.width
.
color of the legend titles
Relative font size for the legend title
font face for the legend title. By default, set to the global parameter fontface
.
font family for the legend title. By default, set to the global parameter fontfamily
.
color of the legend text
Relative font size for the legend text elements
font face for the legend text labels. By default, set to the global parameter fontface
.
font family for the legend text labels. By default, set to the global parameter fontfamily
.
Relative font size for the choropleth histogram
list of formatting options for the legend numbers. Only applicable for layer functions (such as tm_fill
) where labels
is undefined. Parameters are:
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.
Should the labels be formatted scientifically? If so, square brackets are used, and the format
of the numbers is "g"
. Otherwise, format="f"
, and text.separator
, text.less.than
, text.or.more
, and big.num.abbr
are used. Also, the numbers are automatically rounded to millions or billions if applicable.
By default, "f"
, i.e. the standard notation xxx.xxx
, is used. If scientific=TRUE
then "g"
, which means that numbers are formatted scientifically, i.e. n.dddE+nn
if needed to save space.
Number of digits after the decimal point if format="f"
, and the number of significant digits otherwise.
Vector that defines whether and which abbrevations are used for large numbers. It is a named numeric vector, where the name indicated the abbreviation, and the number the magnitude (in terms on numbers of zero). Numbers are only abbrevation when they are large enough. Set it to NA
to disable abbrevations. The default is c("mln" = 6, "bln" = 9)
. For layers where style
is set to log10
or log10_pretty
, the default is NA
.
Character string to use to separate numbers in the legend (default: "to").
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
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
Value that determines how the numbers are aligned, "left"
, "center"
or "right"
Logical that determines whether the text is aligned to three columns (from, text.separator, to). By default FALSE
.
Value that determines how the numbers are aligned, "left"
, "center"
or "right"
Logical that determines whether the text is aligned to three columns (from, text.separator, to). By default FALSE
.
Other arguments passed on to formatC
either a logical that determines whether the legend is placed inside a frame, or a color that directly specifies the frame border color.
line width of the legend frame (applicable if legend.frame
is TRUE
or a color)
Background color of the legend. Use TRUE
to match with the overall background color bg.color
.
Transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the legend.bg.color
is used (normally 1).
Background color of the histogram
Transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the legend.hist.bg.color
is used (normally 1).
Logical that determines whether the title is part of the legend. By default FALSE
, unless the legend is drawn outside the map (see legend.outside
).
Position of the title. Vector of two values, specifying the x and y coordinates. Either this vector contains "left", "LEFT", "center", "right", or "RIGHT" for the first value and "top", "TOP", "center", "bottom", or "BOTTOM" for the second value, or this vector contains two numeric values between 0 and 1 that specifies the x and y coordinates of the tile. The uppercase values correspond to the position without margins (so tighter to the frame).
By default the title is placed on top of the legend (determined by legend.position
).
color of the title
font face for the title. By default, set to the global parameter fontface
.
font family for the title. By default, set to the global parameter fontfamily
.
background color of the title. Use TRUE
to match with the overall background color bg.color
. By default, it is TRUE
if legend.frame
is TRUE
or a color.
Transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the title.bg.color
is used (normally 1).
Logical that determines if the map(s) are shown as panels. If TRUE
, the title will be placed in the panel header instead of inside the map. By default, it is TRUE
when small multiples are created with the by
variable. (See tm_facets
)
Panel labels. Only applicable when panel.show
is TRUE
. For cross tables facets, it should be a list containing the row names in the first, and column names in the second item.
Relative font size of the panel labels
Font color of the panel labels
font face for the panel labels. By default, set to the global parameter fontface
.
font family for the panel labels. By default, set to the global parameter fontfamily
.
Background color of the panel labels
Height of the labels in number of text line heights.
Rotation angles of the panel labels. Vector of two values: the first is the rotation angle (in degrees) of the row panels, which are only used in cross-table facets (when tm_facets
's by
is specified with two variables). The second is the rotation angle of the column panels.
Title that is printed above the map (or small multiples). When multiple pages are generated (see along
argument of tm_facets
), a vector can be provided. By default, the main title is only printed when this along
argument is specified.
Size of the main title
Color of the main title
font face for the main title. By default, set to the global parameter fontface
.
font family for the main title. By default, set to the global parameter fontfamily
.
Position of the main title. Either a numeric value between 0 (left) and 1 (right), or a character value: "left"
, "center"
, or "right"
.
Logical that determines whether the attributes are plot outside of the map/facets.
Character that determines the outside position of the attributes: "top"
or "bottom"
. Only applicable when attr.outside=TRUE
. If the legend is also drawn outside (with legend.outside=TRUE
) and on the same side of the map (e.g. also "top"
or "bottom"
), the attributes are placed between the map and the legend. This can be changed by setting attr.outside.position
to "TOP"
or "BOTTOM"
: in this case, the attributes are placed above respectively below the legend.
Numeric value that determines the relative height of the attribute viewport, when attr.outside=TRUE
.
Position of the map attributes, which are tm_credits
, tm_scale_bar
, tm_compass
, and tm_minimap
. Vector of two values, specifying the x and y coordinates. The first value is "left"
, "LEFT"
, "center"
, "right"
, or "RIGHT"
, and the second value "top"
, "TOP"
, "center"
, "bottom"
, or "BOTTOM"
. The uppercase values correspond to the position without margins (so tighter to the frame). Positions can also be set separately in the map attribute functions. If attr.outside=TRUE
, this argument specifies the position of the attributes within the outside panel.
Justification of the attributes 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/bottom alignment and 1 right/top alignment. This option is only used, if attr.position
is specified by numeric coordinates. It can also be specified per attribute function.
Logical that enables the design mode. If TRUE
, inner and outer margins, legend position, aspect ratio are explicitly shown. Also, feedback text in the console is given.
other arguments from tm_layout
name of the style
name of the format
Predefined styles:
"white" |
White background, commonly used colors (default) |
"gray" /"grey" |
Grey background, useful to highlight sequential palettes (e.g. in choropleths) |
"natural" |
Emulation of natural view: blue waters and green land |
"bw" |
Greyscale, obviously useful for greyscale printing |
"classic" |
Classic styled maps (recommended) |
"cobalt" |
Inspired by latex beamer style cobalt |
"albatross" |
Inspired by latex beamer style albatross |
"beaver" |
Inspired by latex beamer style beaver |
--------------------------- | --------------------------------------------------------------------------------------------------- |
Predefined formats
"World" |
Format specified for world maps |
"World_wide" |
Format specified for world maps with more space for the legend |
"NLD" |
Format specified for maps of the Netherlands |
"NLD_wide" |
Format specified for maps of the Netherlands with more space for the legend |
--------------------------- | --------------------------------------------------------------------------------------------------- |
Tennekes, M., 2018, tmap: Thematic Maps in R, Journal of Statistical Software, 84(6), 1-39, DOI
# NOT RUN {
data(World, land)
tm_shape(World) +
tm_fill("pop_est_dens", style="kmeans", title="Population density") +
tm_style("albatross", frame.lwd=10) + tm_format("World", title="The World")
# }
# NOT RUN {
tm_shape(land) +
tm_raster("elevation", breaks=c(-Inf, 250, 500, 1000, 1500, 2000, 2500, 3000, 4000, Inf),
palette = terrain.colors(9), title="Elevation", midpoint = NA) +
tm_shape(World, is.master=TRUE) +
tm_borders("grey20") +
tm_grid(projection="longlat", labels.size = .5) +
tm_text("name", size="AREA") +
tm_compass(position = c(.65, .15), color.light = "grey90") +
tm_credits("Eckert IV projection", position = c("right", "BOTTOM")) +
tm_style("classic") +
tm_layout(bg.color="lightblue",
inner.margins=c(.04,.03, .02, .01),
earth.boundary = TRUE,
space.color="grey90") +
tm_legend(position = c("left", "bottom"),
frame = TRUE,
bg.color="lightblue")
# }
# NOT RUN {
tm_shape(World, projection="+proj=robin") +
tm_polygons("HPI", palette="div", n=7,
title = "Happy Planet Index") +
tm_credits("Robinson projection", position = c("right", "BOTTOM")) +
tm_style("natural", earth.boundary = c(-180, -87, 180, 87), inner.margins = .05) +
tm_legend(position=c("left", "bottom"), bg.color="grey95", frame=TRUE)
# Example to illustrate the type of titles
tm_shape(World) +
tm_polygons(c("income_grp", "economy"), title = c("Legend Title 1", "Legend Title 2")) +
tm_layout(main.title = "Main Title",
main.title.position = "center",
main.title.color = "blue",
title = c("Title 1", "Title 2"),
title.color = "red",
panel.labels = c("Panel Label 1", "Panel Label 2"),
panel.label.color = "purple",
legend.text.color = "brown")
# }
# NOT RUN {
# global option tmap.style demo
# get current style
current.style <- tmap_style()
qtm(World, fill = "economy", format = "World")
tmap_style("col_blind")
qtm(World, fill = "economy", format = "World")
tmap_style("cobalt")
qtm(World, fill = "economy", format = "World")
# set to current style
tmap_style(current.style)
# }
# NOT RUN {
# TIP: check out these examples in view mode, enabled with tmap_mode("view")
# }
Run the code above in your browser using DataLab