Learn R Programming

tmap (version 1.0)

tm_fill: Draw polygons

Description

Creates a tmap-element that draws the polygons. tm_fill fills the polygons. 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. tm_borders draws the borders of the polygons. tm_polygons fills the polygons and draws the polygon borders.

Usage

tm_fill(col = "grey85", alpha = NA, palette = NULL,
  convert2density = FALSE, area = NULL, n = 5, style = "pretty",
  breaks = NULL, labels = NULL, auto.palette.mapping = TRUE,
  contrast = 1, max.categories = 12, colorNA = "grey60",
  textNA = "Missing", text_separator = "to", text_less_than = "Less than",
  text_or_more = "or more", thres.poly = 1e-05, title = NA,
  legend.is.portrait = TRUE, legend.hist = FALSE, legend.hist.title = NA,
  legend.z = NA, legend.hist.z = NA)

tm_borders(col = "grey40", lwd = 1, lty = "solid", alpha = NA)

tm_polygons(col = "grey85", alpha = NA, border.col = "grey40",
  border.alpha = 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. By default, "RdYlGn" is taken for numeric variables and "Dark2" for categorical variables.
convert2density
boolean that determines whether col is converted to a density variable. Should be TRUE when col consists of absolute numbers. The area size is either approximated from the shape object, or given by the argument
area
Name of the data variable that contains the area sizes in squared kilometer.
n
preferred number of classes (in case col is a numeric variable)
style
method to cut the color scale (in case col is a numeric variable): e.g. "fixed", "equal", "pretty", "quantile", or "kmeans". See the details in classIntervals.
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
textNA
text used for missing values. Use NA to omit text for missing values in the legend
text_separator
Character string to use to separate numbers in the legend (default: "to").
text_less_than
Character string to use to translate "Less than" (which is the default).
text_or_more
Character string to use to translate "or more" (which is the default).
thres.poly
number that specifies the threshold at which polygons are taken into account. The number itself corresponds to the proportion of the area sizes of the polygons to the total polygon size.
title
title of the legend element
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
lwd
border line width (see par)
lty
border line type (see par)
border.col
border line color
border.alpha
transparency number between 0 (totally transparent) and 1 (not transparent). By default, the alpha value of the col is used (normally 1).
...
arguments passed to either tm_fill or tm_borders

Value

See Also

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

Examples

Run this code
data(World)
data(Europe)
data(NLD_muni)
data(NLD_prov)

# Constant fill
tm_shape(World) + tm_fill("darkolivegreen3") + tm_layout_World(title="A green World")

# Borders only
tm_shape(Europe) + tm_borders()

# Data variable containing colours values
Europe$isNLD <- ifelse(Europe$name=="Netherlands", "darkorange", "darkolivegreen3")
tm_shape(Europe) +
    tm_fill("isNLD") +
tm_layout("Find the Netherlands!")

# Numeric data variable
tm_shape(NLD_muni) +
    tm_fill(col="population", convert2density=TRUE, 
        style="kmeans", title="Population (per km2)", legend.hist=TRUE) +
    tm_borders("grey25", alpha=.5) + 
tm_shape(NLD_prov) +
    tm_borders("grey40", lwd=2) +
tm_layout_NLD_wide(bg.color="white", draw.frame = FALSE, legend.hist.bg.color="grey90")

tm_shape(Europe) +
    tm_polygons("gdp_cap_est", style="kmeans", textNA = "Non-European countries", 
        title="GDP per capita") +
    tm_text("iso_a3", size="AREA", root=4, scale=2) +
tm_layout_Europe()

tm_shape(World) +
    tm_polygons("pop_est_dens", style="kmeans", palette="YlOrRd", title="Population per km2") +
    tm_text("iso_a3", size="AREA", scale=1.5) +
tm_layout_World()

# Categorical data variable
tm_shape(World) +
    tm_polygons("income_grp", palette="-Blues", title="Income classification") +
    tm_text("iso_a3", size="AREA", scale=1.5) +
tm_layout_World()

tm_shape(NLD_prov) + 
    tm_fill("name") + 
tm_shape(NLD_muni) + 
    tm_borders() + 
tm_shape(NLD_prov) + 
    tm_borders(lwd=2) +
    tm_text("name", shadow=TRUE) +
tm_layout_NLD("Provinces and municipalities", legend.show=FALSE, bg.color="white")

Run the code above in your browser using DataLab