Draw a raster

Creates a tmap-element that draws a raster. For coloring, there are three options: 1) a fixed color is used, 2) a color palette is mapped to a data variable, 3) RGB values are used. The function tm_raster is designed for option 2, while tm_rgb is used for option 3.

tm_raster(col = NA, alpha = NA, palette = NULL, n = 5,
  style = ifelse(is.null(breaks), "pretty", "fixed"), breaks = NULL,
  interval.closure = "left", labels = NULL, auto.palette.mapping = TRUE,
  contrast = NA, max.categories = 12, colorNA = NULL, saturation = 1,
  interpolate = FALSE, textNA = "Missing", showNA = NA, title = NA, = TRUE, legend.format = list(), = TRUE,
  legend.reverse = FALSE, legend.hist = FALSE, legend.hist.title = NA,
  legend.z = NA, legend.hist.z = NA)

tm_rgb(alpha = NA, saturation = 1, interpolate = TRUE, ...)


three options: a single color value, the name of a data variable that is contained in shp, or the name of a variable in shp that contain color values. In the second case the values (numeric or categorical) that will be depicted by a color palette (see palette. If omitted, and if shp contains three numeric layers that range between 0 and 255, these are interpreted as RGB values, else, the first data variable is selected. If multiple values are specified, small multiples are drawn (see details). By default, it is the name of the first data variable.


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


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.


preferred number of classes (in case col is a numeric variable)


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


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.


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


labels of the classes


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.


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


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.


color used for missing values. Use NULL for transparency.


Number that determines how much saturation (also known as chroma) is used: saturation=0 is greyscale and saturation=1 is normal. This saturation value is multiplied by the overall saturation of the map (see tm_layout).


Should the raster image be interpolated? By default FALSE for tm_raster and TRUE for tm_rgb.


text used for missing values.


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


title of the legend element

logical that determines whether the legend is shown


list of formatting options for the legend numbers. Only applicable if 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 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.


By default, "f", i.e. the standard notation, is used. If scientific=TRUE then "g", which means that numbers are formatted scientically, 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.


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 = 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 = TRUE


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

. By default "left" for legends in portrait format ( = TRUE), and "center" otherwise.

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


Other arguments passed on to formatC

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


logical that determines whether the items of the legend regarding the text sizes are shown in reverse order, i.e. from bottom to top when = TRUE and from right to left when = FALSE


logical that determines whether a histogram is shown


title for the histogram. By default, one title is used for both the histogram and the normal legend.


index value that determines the position of the legend element 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.


index value that determines the position of the histogram legend element


arguments passed on from tm_raster to tm_rgb


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. The aesthetic argument of tm_raster is col. 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.



See Also


  • tm_raster
  • tm_rgb
data(World, land, metro)

pal8 <- c("#33A02C", "#B2DF8A", "#FDBF6F", "#1F78B4", "#999999", "#E31A1C", "#E6E6E6", "#A6CEE3")
tm_shape(land, ylim = c(-88,88)) +
    tm_raster("cover_cls", palette = pal8, title = "Global Land Cover") +
tm_shape(metro) + tm_dots(col = "#E31A1C") +
tm_shape(World) +
    tm_borders(col = "black") +
tm_layout(scale = .8, 
	legend.position = c("left","bottom"), = "white", = .2, 
    legend.frame = "gray50")

# }
pal20 <- c("#003200", "#3C9600", "#006E00", "#556E19", "#00C800", "#8CBE8C",
		   "#467864", "#B4E664", "#9BC832", "#EBFF64", "#F06432", "#9132E6",
		   "#E664E6", "#9B82E6", "#B4FEF0", "#646464", "#C8C8C8", "#FF0000",
		   "#FFFFFF", "#5ADCDC")
tm_shape(land) +
	tm_raster("cover", max.categories = 20, palette = pal20, title = "Global Land Cover") + 
	tm_layout(scale=.8, legend.position = c("left","bottom"))
# }

tm_shape(land, ylim = c(-88,88)) +
    tm_raster("trees", palette = "Greens", title = "Percent Tree Cover") +
tm_shape(World) +
    tm_borders() +
tm_layout(legend.position = c("left", "bottom"), bg.color = "lightblue")

# TIP: check out these examples in view mode, enabled with tmap_mode("view")

# }
# doesn't work in view mode, since it does not support small multiples
tm_shape(land) +
	tm_raster("black") +
# }
Documentation reproduced from package tmap, version 1.11, License: GPL-3

Community examples

Looks like there are no examples yet.