Last chance! 50% off unlimited learning
Sale ends in
tmap-element
that specifies facets (small multiples). Small multiples can be created in two ways: 1) by specifying the by
argument with one or two variable names, by which the data is grouped, 2) by specifying multiple variable names in any of the aesthetic argument of the layer functions (for instance, the argument col
in tm_fill
). This function further specifies the facets, for instance number of rows and columns, and whether the coordinate and scales are fixed or free (i.e. independent of each other).
tm_facets(by = NULL, ncol = NULL, nrow = NULL, free.coords = FALSE, drop.units = free.coords, drop.empty.facets = TRUE, showNA = NA, textNA = "Missing", free.scales = is.null(by), free.scales.fill = free.scales, free.scales.bubble.size = free.scales, free.scales.bubble.col = free.scales, free.scales.text.size = free.scales, free.scales.text.col = free.scales, free.scales.line.col = free.scales, free.scales.line.lwd = free.scales, free.scales.raster = free.scales, inside.original.bbox = FALSE, scale.factor = 2, drop.shapes = drop.units)
by
contains two variable names.by
contains two variable names.by
argument is specified, should each map has its own coordinate ranges?by
argument is specified, should non-selected spatial units be dropped? If FALSE
, they are plotted where mapped aesthetics are regared as missing values. By default, TRUE
if free.coords=TRUE
. Not applicable for raster shapes.by
argument is specified, should empty facets be dropped? Empty facets occur when the by
-variable contains unused levels. When TRUE
and two by
-variables are specified, empty rows and colums are dropped.by
argument is specified, should missing values of the by
-variable be shown in a facet? If two by
-variables are specified, should missing values be shown in an additional row and column? If NA
, missing values only are shown if they exist. Similar to the useNA
argument of table
, where TRUE
, FALSE
, and NA
correspond to "always"
, "no"
, and "ifany"
respectively.tm_fill
, color and size from tm_bubbles
and line color from tm_lines
.free.coords
, should the bounding box of each small multiple be inside the original bounding box?scale.factor
th root of the scaling factor of the shapes. So, for scale.factor=1
, they are scaled proportional to the scaling of the shapes. Since elements, especially text, are often too small to read, a higher value is recommended. By default, scale.factor=2
.drop.units
vignette("tmap-nutshell")
data(World, Europe, NLD_muni, NLD_prov, land, metro)
current.mode <- tmap_mode("plot") # small multiples don't work in view mode
# Facets defined by constant values
tm_shape(World) +
tm_fill(c("forestgreen", "goldenrod")) +
tm_format_World(title=c("A green world", "A dry world"), bg.color="lightskyblue2",
title.position=c("left", "bottom"))
# Facets defined by multiple variables
tm_shape(Europe) +
tm_polygons(c("well_being", "life_exp"),
style=c("pretty", "fixed"), breaks=list(NULL, c(65,70,75,80,85)),
palette=list("Oranges", "Purples"),
border.col = "black",
title=c("Well-Being Index", "Population Density")) +
tm_format_Europe()
## Not run:
# tm_shape(NLD_muni) +
# tm_fill(c("pop_0_14", "pop_15_24", "pop_25_44", "pop_45_64", "pop_65plus"),
# style="kmeans",
# palette=list("Oranges", "Greens", "Blues", "Purples", "Greys"),
# title=c("Population 0 to 14", "Population 15 to 24", "Population 25 to 44",
# "Population 45 to 64", "Population 65 and older")) +
# tm_shape(NLD_prov) +
# tm_borders() +
# tm_format_NLD(frame = TRUE, asp=0)
# ## End(Not run)
# Facets defined by groupings
tm_shape(NLD_prov) +
tm_borders() +
tm_fill("gold2") +
tm_facets(by="name") +
tm_layout()
tm_shape(NLD_prov) +
tm_fill("gold2") + tm_borders() +
tm_facets(by="name", free.coords = TRUE, drop.units=TRUE) +
tm_layout()
## Not run:
# tm_shape(NLD_muni) +
# tm_borders() +
# tm_facets(by="province") +
# tm_fill("population", style="kmeans", convert2density = TRUE) +
# tm_shape(NLD_prov) +
# tm_borders(lwd=4) +
# tm_facets(by="name", free.coords=TRUE, drop.units=TRUE)
#
# tm_shape(land) +
# tm_raster("black") +
# tm_facets(by="cover_cls")
# ## End(Not run)
# Facets defined by groupings defined by two variables
## Not run:
# World$HPI3 <- cut(World$HPI, breaks = c(20, 35, 50, 65),
# labels = c("HPI low", "HPI medium", "HPI high"))
# World$GDP3 <- cut(World$gdp_cap_est, breaks = c(0, 5000, 20000, Inf),
# labels = c("GDP low", "GDP medium", "GDP high"))
#
# tm_shape(World) +
# tm_fill("HPI3", palette="Dark2", colorNA="grey90", legend.show = FALSE) +
# tm_facets(c("HPI3", "GDP3"), showNA=FALSE)
#
# metro$pop1950cat <- cut(metro$pop1950, breaks=c(0.5, 1, 1.5, 2, 3, 5, 10, 40)*1e6)
# metro$pop2020cat <- cut(metro$pop2020, breaks=c(0.5, 1, 1.5, 2, 3, 5, 10, 40)*1e6)
#
# tm_shape(World) +
# tm_fill() +
# tm_shape(metro) +
# tm_dots("red") +
# tm_facets(c("pop1950cat", "pop2020cat")) +
# tm_layout(panel.label.rot = c(0, 90), panel.label.size = 2)
# ## End(Not run)
# example: Meuse data
## Not run:
# library(sp)
# library(OpenStreetMap)
# data(meuse)
# coordinates(meuse) <- ~x+y
# proj4string(meuse) <- get_proj4("rd")
#
# meuse_osm <- read_osm(bb(meuse, ext=1.1, current.projection = "rd", projection = "longlat"))
#
# qtm(meuse_osm) +
# tm_shape(meuse) +
# tm_bubbles(size=c("cadmium", "copper", "lead", "zinc"),
# col=c("orange", "orange3", "grey40", "grey70"),
# border.col="black",
# border.alpha = .75,
# scale=.7)
# ## End(Not run)
# restore current mode
tmap_mode(current.mode)
Run the code above in your browser using DataLab