knitr::opts_chunk$set(echo = TRUE, fig.width = 9)
Open the tmap repository as RStudio project. Set Knit Directory (via drop-down menu of the Knit button in RStudio) to Project Directory.
library(devtools) load_all() data(World,metro,land) World$pop_class = cut(World$pop_est, breaks = c(0, 10, 100, 1000, Inf) * 1e6, labels = c("Small", "Medium", "Large", "Extra Large")) World$HPI_class = cut(World$HPI, breaks = seq(10, 50, by = 10)) World$well_being_class = cut(World$well_being, breaks = seq(2, 8, by = 2)) World$footprint_class = cut(World$footprint, breaks = seq(0, 16, by = 4)) metro$pop2020_class = cut(metro$pop2020, breaks = c(.5, 1.5, 2.5, 5, 15, 40) * 1e6) Africa = World[World$continent == "Africa", ] tmapV = ifelse(packageVersion("tmap") >= "3.4", "4", "3") txt = function(x) print(grid::grid.text(x, gp = gpar(cex = 3))) v3 = function(e) { if (tmapV == "3") { print(e) } else { txt("Only for tmap 3") } invisible(NULL) } v4 = function(e) { if (tmapV == "4") { print(e) } else { txt("Only for tmap 4") } invisible(NULL) } txt(paste("Loaded tmap version", tmapV))
These tests focus on dealing with shapes when facets are created. Legends, scales will be studied in other tests.
tm_shape(World) + tm_polygons("HPI")
# one polygon shape, two aes values tm_shape(World) + tm_polygons(c("HPI", "income_grp"))
# split polygon shape (by) tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "continent")
# split polygon shape (by), with fixed coords tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "continent", free.coords = FALSE)
# 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"))
# 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 v4({ tm_shape(World) + tm_polygons("HPI", lty = "economy", fill.free = FALSE, lty.free = TRUE) + tm_facets(by = "continent", free.coords = FALSE, drop.units = FALSE) })
# nothing to drop tm_shape(World) + tm_polygons(c("HPI", "economy")) + tm_facets(free.coords = FALSE, drop.units = TRUE)
# 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)
# 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)
# nothing to drop tm_shape(land) + tm_raster("cover_cls")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.