Skip to contents

Facetting shapes

These tests focus on dealing with shapes when facets are created. Legends, scales will be studied in other tests.

Polygons

One facet

tm_shape(World) +
    tm_polygons("HPI")

Multiple facets, specified with multiple variables

# one polygon shape, two aes values
tm_shape(World) +
    tm_polygons(c("HPI", "income_grp"))
#> [plot mode] fit legend/component: Some legend items or map compoments do not
#> fit well, and are therefore rescaled.
#>  Set the tmap option `component.autoscale = FALSE` to disable rescaling.

Multiple facets, specified by a ‘facet wrap’

# split polygon shape (by)
tm_shape(World) +
    tm_polygons("HPI") +
    tm_facets(by = "continent")

Multiple facets, specified by a ‘facet wrap’, with fixed coordinates

# split polygon shape (by), with fixed coords
tm_shape(World) +
    tm_polygons("HPI") +
    tm_facets(by = "continent", free.coords = FALSE)

Filter

# filter polygons (filtered out will get value.null)
tm_shape(World, filter = World$pop_est > 100e6) +
    tm_polygons(c("blue", "red"))

tm_shape(World, filter = World$pop_est > 100e6) +
    tm_polygons("HPI")

tm_shape(World, filter = World$pop_est > 100e6) +
    tm_polygons(c("HPI", "economy"))
#> [plot mode] fit legend/component: Some legend items or map compoments do not
#> fit well, and are therefore rescaled.
#>  Set the tmap option `component.autoscale = FALSE` to disable rescaling.

Drop units

# fixed aes, wrap
tm_shape(World) +
    tm_polygons("blue") +
    tm_facets(by = "continent", free.coords = FALSE, drop.units = FALSE)

# one aesthetics, different frees
tm_shape(World) +
    tm_polygons("HPI") +
    tm_facets(by = "continent", free.coords = FALSE, drop.units = FALSE)

# multiple aesthetics, different frees
tm_shape(World) +
    tm_polygons("HPI", lty = "economy", fill.free = FALSE, lty.free = TRUE) +
    tm_facets(by = "continent", free.coords = FALSE, drop.units = FALSE)
#> The visual variable "lty" of the layer "polygons" contains a unique value. Therefore a categorical scale is applied (tm_scale_categorical).
#> The visual variable "lty" of the layer "polygons" contains a unique value. Therefore a categorical scale is applied (tm_scale_categorical).
#> [plot mode] fit legend/component: Some legend items or map compoments do not
#> fit well, and are therefore rescaled.
#>  Set the tmap option `component.autoscale = FALSE` to disable rescaling.

# nothing to drop
tm_shape(World) +
    tm_polygons(c("HPI", "economy")) +
    tm_facets(free.coords = FALSE, drop.units = TRUE)
#> [plot mode] fit legend/component: Some legend items or map compoments do not
#> fit well, and are therefore rescaled.
#>  Set the tmap option `component.autoscale = FALSE` to disable rescaling.

Drop empty facets

# default drop empty (wrap) facets
tm_shape(Africa) +
    tm_polygons("blue") +
    tm_facets(by = "income_grp")


tm_shape(Africa) +
    tm_polygons("HPI") +
    tm_facets(by = "income_grp")

# keep empty (wrap) facets
tm_shape(Africa) +
    tm_polygons("blue") +
    tm_facets(by = "income_grp", drop.empty.facets = FALSE)


tm_shape(Africa) +
    tm_polygons("HPI") +
    tm_facets(by = "income_grp", drop.empty.facets = FALSE)

# default drop empty (xtab) facet: only if whole row or column is empty
tm_shape(Africa) +
    tm_polygons("pop_est_dens") +
    tm_facets_grid("income_grp", "well_being_class", drop.units = FALSE)

# keep empty (xtab) facets
tm_shape(Africa) +
    tm_polygons("pop_est_dens") +
    tm_facets_grid("income_grp", "well_being_class", drop.units = FALSE, drop.empty.facets = FALSE)

Drop NA facets

# default keep NA facets (where by variable is NA)
tm_shape(Africa) +
    tm_polygons("blue") +
    tm_facets(by = "footprint_class")

# drop NA facets 
tm_shape(Africa) +
    tm_polygons("blue") +
    tm_facets(by = "footprint_class", drop.NA.facets = TRUE)

# default keep NA facets (where by variable is NA)
tm_shape(Africa) +
    tm_polygons("pop_est_dens") +
    tm_facets(by = "footprint_class")

# drop NA facets 
tm_shape(Africa) +
    tm_polygons("pop_est_dens") +
    tm_facets(by = "footprint_class", drop.NA.facets = TRUE)

# default keep NA facets (where by variable is NA)
tm_shape(Africa) +
    tm_polygons("pop_est_dens") +
    tm_facets_grid("footprint_class", "HPI_class")

# drop NA facets 
tm_shape(Africa) +
    tm_polygons("pop_est_dens") +
    tm_facets_grid("footprint_class", "HPI_class", drop.NA.facets = TRUE)

Rasters

# nothing to drop
tm_shape(land) +
    tm_raster("cover_cls")