```r

knitr::opts_chunk$set(echo = TRUE, fig.width = 9)

# Instructions

Open the tmap repository as RStudio project.
Set Knit Directory (via drop-down menu of the Knit button in RStudio) to Project Directory.

```r
library(devtools)
load_all()
library(stars)
library(terra)

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)
metro$pop2020_class8 = cut(metro$pop2020, breaks = c(.5, 1, 1.5, 2.5, 5, 10, 15, 20, 40) * 1e6)



Africa = World[World$continent == "Africa", ]

data(land)

land$cover_int = as.integer(land$cover)
land$cover_int[1:1000] = NA

World$economy_int = as.integer(World$economy)
World$economy_int[1:100] = NA


landsat_stars = read_stars(system.file("raster/landsat.tif", package = "spDataLarge"))
landsat_terra = rast(system.file("raster/landsat.tif", package = "spDataLarge"))

tif_stars = system.file("tif/L7_ETMs.tif", package = "stars") %>% stars::read_stars()
tif_terra = system.file("tif/L7_ETMs.tif", package = "stars") %>% terra::rast()

prec = stars::read_ncdf(system.file("nc/test_stageiv_xyt.nc", package = "stars"), curvilinear = c("lon", "lat"), ignore_bounds = TRUE)
nc = sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"), "nc.gpkg") %>%
    sf::st_transform(sf::st_crs(prec))
prec_nc = aggregate(prec, by = nc, FUN = max)

weather = stars::read_ncdf(system.file('nc/bcsd_obs_1999.nc', package = 'stars'))
weather1 = stars::st_set_dimensions(merge(weather), names = c('longitude','latitude','time','attributes'))
weather2 = split(weather1, 'time')




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))

Facetting shapes (variables)

These tests focus on dealing with creating facets with variables, group by variables or (stars) dimensions etc.

One facet

tm_shape(World) +
    tm_polygons("HPI")

Wraps

# wrap over variables
tm_shape(World) +
    tm_polygons(c("HPI", "income_grp"))

Multiple facets, specified by a 'facet wrap'

# wrap by
tm_shape(World) +
    tm_polygons("HPI") +
    tm_facets(by = "continent")
# conflict: should take first variable
tm_shape(World) +
    tm_polygons(c("HPI", "income_grp")) +
    tm_facets(by = "continent")
tm_shape(World) +
    tm_polygons(c("HPI", "income_grp")) +
    tm_facets_grid(columns = "continent")
tm_shape(World) +
    tm_polygons(c("HPI", "income_grp")) +
    tm_facets_grid(rows = "continent")
tm_shape(World) +
    tm_polygons(c("HPI")) +
    tm_facets_grid(columns = "well_being_class", rows = "continent")
# stars 2 attributes and 3th dimensions
tm_shape(weather) +
    tm_raster()
# idem
tm_shape(weather) +
    tm_raster(c("tas", "pr"))
# inverse grid
tm_shape(weather) +
    tm_raster() +
    tm_facets_grid(rows = "time")
# idem
tm_shape(weather) +
    tm_raster(c("tas", "pr")) +
    tm_facets_grid(rows = "time")
# idem
tm_shape(weather) +
    tm_raster(c("tas", "pr")) +
    tm_facets_grid(columns = "VARS__")
# wrap with warning (only first variable used)
tm_shape(weather) +
    tm_raster() +
    tm_facets_wrap()
# select other variable, wrap (default to shape dim)
tm_shape(weather) +
    tm_raster("tas") +
    tm_facets_wrap()
# idem
tm_shape(weather) +
    tm_raster("tas")
# idem
tm_shape(weather) +
    tm_raster(c("tas", "pr")) +
    tm_facets_grid(rows = "time")
tm_shape(weather2) +
    tm_raster()
weather22 = weather2[1:2]

tm_shape(weather22) +
    tm_raster(col.free = c(T, F))

tm_shape(weather22) +
    tm_raster(col.free = c(F, F))

tm_shape(weather22) +
    tm_raster(col.free = c(T, T))

tm_shape(weather22) +
    tm_raster(col.free = c(F, T))
tm_shape(World) + tm_polygons("pop_est", fill.free = FALSE) + tm_facets("continent")
tm_shape(World) + tm_polygons("pop_est", fill.free = TRUE) + tm_facets("continent")
tm_shape(World) + tm_polygons("pop_est", fill.free = TRUE) + tm_facets("continent", nrows = 1)
# merging facets OK
tm_shape(World) +
    tm_polygons(c("area", "pop_est", "pop_est_dens", "economy")) +
tm_shape(World) +
    tm_symbols(size = "pop_est") +
    tm_facets(by = "pop_class")
# merging facets OK
tm_shape(World) +
    tm_polygons(c("area", "pop_est", "pop_est_dens", "economy")) +
tm_shape(World) +
    tm_symbols(size = "pop_est") +
    tm_facets(by = "pop_class", nrows = 1)
tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "economy")
tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "economy_int")
tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "economy", drop.NA.facets = TRUE)
tm_shape(land) + tm_raster("trees") + tm_facets(by = "cover_int")
tm_shape(land) + 
    tm_raster("trees") +
    tm_facets_wrap("cover_cls") +
tm_shape(World) +
    tm_polygons(col = rainbow(8), lwd = 3, fill = NA) +
tm_shape(metro) +
    tm_symbols(fill = "pop2020_class8", size = .5)


r-tmap/tmap documentation built on July 17, 2024, 5:04 p.m.