15  Facets

library(sf)
library(tmap)
slo_borders = read_sf("data/slovenia/slo_border.gpkg")
slo_regions = read_sf("data/slovenia/slo_regions.gpkg")
slo_regions_ts = read_sf("data/slovenia/slo_regions_ts.gpkg")
slo_regions_ts = subset(slo_regions_ts, time %in% c("2006", "2010", "2014", "2018", "2022"))
tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions) +
  tm_fill(c("pop_dens", "gdppercap", "tourism"))
#> [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.

tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets(by = "time", ncol = 5)
#> [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.

tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_fill(c("pop_dens", "gdppercap", "tourism")) +
  tm_facets(by = "time")
#> [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.

tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets(by = "region_group", ncol = 5)
#> [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.

tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets(by = "region_group", ncol = 5) +
  tm_shape(slo_borders) +
  tm_borders()
#> [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.

tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets(by = "region_group", ncol = 5, free.coords = FALSE) +
  tm_shape(slo_borders) +
  tm_borders()
#> [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.

tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets_wrap(by = "region_group", ncol = 5)
#> [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.

tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets_grid(rows = "region_group", columns = "time")

tm_shape(slo_borders) +
  tm_borders() +
  tm_shape(slo_regions_ts) +
  tm_fill("gdppercap") +
  tm_facets_grid(rows = "region_group", columns = "time") +
  tm_xlab("Year", side = "top", size = 2, space = 1) +
  tm_ylab("Region", rotation = 90, size = 2, space = 1)

library(stars)
slo_tavg = read_stars("data/slovenia/slo_tavg.tif")
slo_elev = read_stars("data/slovenia/slo_elev.tif")
tm_shape(slo_tavg) +
  tm_raster() +
  tm_facets(by = "band", ncol = 4)
#> [scale] tm_raster:() the data variable assigned to 'col' contains positive and negative values, so midpoint is set to 0. Set 'midpoint = NA' in 'fill.scale = tm_scale_intervals(<HERE>)' to use all visual values (e.g. colors)

tm_shape(slo_tavg) +
  tm_raster(
    col.scale = tm_scale_continuous(values = "viridis"),
    col.legend = tm_legend(title = "Temperature (°C)"),
    col.free = TRUE) +
  tm_facets(by = "band", ncol = 4)
#> [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.

tm_shape(slo_elev) +
  tm_raster() +
  tm_shape(slo_regions, is.main = TRUE) + 
  tm_borders() +
  tm_facets(by = "region_group", free.coords = TRUE)
#> [scale] tm_raster:() the data variable assigned to 'col' contains positive and negative values, so midpoint is set to 0. Set 'midpoint = NA' in 'fill.scale = tm_scale_intervals(<HERE>)' to use all visual values (e.g. colors)

tm_shape(slo_regions, is.main = TRUE) + 
  tm_borders() +
  tm_facets(by = "region_group", free.coords = TRUE) +
  tm_shape(slo_elev) +
  tm_raster()