Learn R Programming

tmap (version 1.2)

tm_raster: Draw a raster

Description

Creates a tmap-element that draws a raster. Either a fixed color is used, or a color palette is mapped to a data variable. By default, a divering color palette is used for numeric variables and a qualitative palette for categorical variables.

Usage

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

Arguments

col
either a single color value or the name of a data variable that is contained in shp. In the latter case, either the data variable contains color values, or values (numeric or categorical) that will be depicted by a color palette (see pa
alpha
transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the col is used (normally 1).
palette
palette name. See RColorBrewer::display.brewer.all() for options. Use a "-" as prefix to reverse the palette. The default palette is taken from tm_layout's argument aes.pal
n
preferred number of classes (in case col is a numeric variable)
style
method to process the color scale when col is a numeric variable. Discrete options are "cat", "fixed", "sd", "equal", "pretty", "quantile", "kmeans", <
breaks
in case style=="fixed", breaks should be specified
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 respect
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 t
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, then levels are combined.
colorNA
color used for missing values
saturation
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
textNA
text used for missing values. Use NA to omit text for missing values in the legend
title
title of the legend element
legend.show
logical that determines whether the legend is shown
legend.format
list of formatting options for the legend numbers. Only applicable if labels is undefined. Parameters are: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
legend.is.portrait
logical that determines whether the legend is in portrait mode (TRUE) or landscape (FALSE)
legend.hist
logical that determines whether a histogram is shown
legend.hist.title
title for the histogram. By default, one title is used for both the histogram and the normal legend.
legend.z
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.
legend.hist.z
index value that determines the position of the histogram legend element

Value

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. The aesthetic argument of tm_raster is col. In the latter case, the arguments, except for thres.poly, and 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

../doc/tmap-nutshell.html{vignette("tmap-nutshell")}

Examples

Run this code
data(World, land)

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

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(World) +
    tm_borders(col="black") +
tm_layout(scale=.8, 
	legend.position = c("left","bottom"),
    legend.bg.color = "white", legend.bg.alpha=.2, 
    legend.frame="gray50")

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

tm_shape(land) +
	tm_raster("black") +
	tm_facets(by="cover_cls") +
	tm_layout(title.position = c("left", "bottom"), title.bg.color="gray80")

Run the code above in your browser using DataLab