
Cheat sheet: Regions and spatial hierarchies
Source:vignettes/articles/cheatsheet-geoscales.Rmd
cheatsheet-geoscales.RmdA GeoScale holds leaf regions and the geoframes that group them, with weights, labels and optional geometry; data is recast between geoframes through the leaves
library(geoscales)
gs <- geoscale_example()
geoscale_leaftable(gs)
country state zone cell km2 pop 1 N N1 N1 A1 100 10 2 N N1 N1 A2 200 90 3 N N2 ZB A3 300 30 4 N N2 ZB A4 400 70 5 S S1 ZB A5 500 50 6 S S1 ZC A6 600 50 7 <NA> <NA> <NA> ROW 1000 0
NA: no code at that geoframe.Three awkward features of real region tables: N1 is both a state and a zone, zone ZB straddles two states, leaf ROW belongs to no country.
gg_geoscale(gs): the icicle; widths = weight shares; no map needed"stack", view = "isometric", rotate = 180; gm: the example on squares (Geometry)| leaf | a finest region: one row of the leaftable |
| geoframe | one resolution: a column over the leaves, ordered coarsest first |
| region | a member of a geoframe; codes are authored and may repeat across geoframes |
| leaf column | the finest geoframe, unless leaf = names a finer column |
| weight | a numeric leaf column (area, population); one is the default |
| share | a weight as a fraction of the whole or of a parent |
| nesting | not required: every recast routes through the leaves |
| label | a display name per geoframe, region and variant |
| hidden | a geoframe kept for calculation, not plotted |
| coverage | the share of the parent's weight a subset keeps |
The object holds no data: tables stay ordinary, tied to it by region-code columns.
tour <- geoscale_from_leaftable(
data.frame(
country = c("N", "N", "N", "S"),
state = c("N1", "N1", "N2", "S1"),
cell = c("A1", "A2", "A3", "A4"),
km2 = c(100, 200, 300, 400)),
geoframes = c("country", "state",
"cell"),
name = "tour")
"" in a geoframe column reads as NA; rows are sorted by geoframe unless row_order = "given".geoscale_build(..., geoframes, weights, name) pyNo maps are bundled: a provider fetches a wide table whose columns become geoframes and weights.
ne_geoscale(scale, geoframes, weights, …) pypop_est, gdp_md.ne_source(scale, geoframe, country, …) pysf table: "country" (admin-0) or "states" (admin-1, needs rnaturalearthhires).geoscale_from_provider(provider, geoframes, weights, name, …) pysf passed directly.world <- ne_geoscale(scale = 50)
isl <- ne_source(geoframe = "states",
country = "Iceland")
register_geoscale_provider("toy",
fetch = function(...) data.frame(
top = "T", cell = c("a", "b"),
km2 = c(1, 2)),
geoframes = c("top", "cell"))
toy <- geoscale_from_provider("toy")geoscale_id(gs) py"name@hash": the hash covers geoframes and memberships, not weights, labels or geometry.summary(gs): weight totals, coverage, nesting, geometry. gs@meta, gs@members: the S7 properties.
A bare code is ambiguous: every lookup names its geoframe. Ancestry goes through the leaves, so a region may have two parents.
geoscale_children(gs, "state", "N1")
#> "N1" (the zone)
geoscale_children(gs, "zone", "N1")
#> "A1" "A2"
geoscale_parents(gs, "zone", "ZB",
target = "state")
#> "N2" "S1"
geoscale_share(x, geoframe, weight, within) pywithin region.geoscale_add_weight(x, ..., default) pygl <- attach_labels_geoscale(gs,
data.frame(geoframe = "country",
region = c("N", "S"),
label = c("North", "South")),
variant = "name")
geoscale_label(gl, c("N", "S"),
"country")
[1] "North" "South"
geoscale_labels(x, geoframe, variant) pygeoscale_label() returns the code where no label exists.hide_geoframes_geoscale(x, geoframes) pygeoscale_id() unchanged. show_geoframes_geoscale() undoes it.n <- filter_geoscale(gs, "country", "N")
geoscale_coverage(n)
km2 pop 0.3225806 0.6666667
example[country:N], with the kept share of the root's weight totals.prune_geoscale(x, geoframe, keep_geometry) pyexample@state.zoom_geoscale(x, focus, geoframes, name, …) pyz <- zoom_geoscale(gs, focus = "A1",
geoframes = c("country", "state"))
geoscale_regions(z, "zoom")
[1] "A1" "N1_rest" "N_rest" "S"
z: country, zoom, cell.Optional: one shape per leaf, in leaftable row order. Needs sf.
library(sf)
sq <- function(x, y, s = 1) st_polygon(
list(cbind(x + s * c(0, 1, 1, 0, 0),
y + s * c(0, 0, 1, 1, 0))))
shp <- st_sf(
cell = c("A1", "A2", "A3", "A4",
"A5", "A6", "ROW"),
geometry = st_sfc(sq(0, 1),
sq(0, 0), sq(1, 1), sq(1, 0),
sq(2, 1), sq(2, 0),
sq(2.2, 2.2, s = 0.6)))
gm <- attach_geometry_geoscale(gs,
shp, by = "cell")
attach_geometry_geoscale(x, geom, by, geoframe, positional) pysf is matched on its code column by. A bare sfc binds by position, refused when the constructor re-sorted the rows unless positional = TRUE.check_geometry_geoscale(x, reference, by, …)reference, evidence without.pts <- data.frame(lon = c(0.5, 2.5, 9),
lat = c(1.5, 0.5, 9))
coords_to_region(pts, gm, "zone",
crs = NA)
[1] "N1" "ZC" NA
NA: outside every region. crs = NA: the squares carry no CRS.geoscale_adjacent(x, geoframe, within, contiguity, …)"queen" (corners count) or "rook".geoscale_adjacent(gm, "state")
N1 N2 S1 N1 FALSE TRUE FALSE N2 TRUE FALSE TRUE S1 FALSE TRUE FALSE
recast_geoscale(data, gs, source, target, values, rule, weight, na_action, …) pyx <- data.frame(
cell = c("A1", "A2", "A3",
"A4", "A5", "A6"),
capacity = 1:6)
recast_geoscale(x, gs, source = "cell",
target = "zone", rule = "sum")
zone capacity 1 N1 3 2 ZB 12 3 ZC 6
ZB = A3 + A4 + A5, across two states.y <- data.frame(country = c("N", "S"),
capacity = c(10, 20))
recast_geoscale(y, gs,
target = "state", rule = "sum",
weight = "km2")
state capacity 1 N1 3 2 N2 7 3 S1 20
sum splits by the weight; totals conserve. source is inferred from the column names.recast_geoscale(x, gs,
target = "country",
rule = "weighted_mean",
weight = "pop")
country capacity 1 N 2.8 2 S 5.5
Other columns (year, technology) ride along as identifiers; a numeric one needs values = to name the value columns. Results list every member of target, NA where nothing landed.
| rule | up | down |
sum |
sum | split by weight |
weighted_mean |
weighted mean | copy |
mean |
mean | copy |
copy |
common value, or error | copy |
sd |
std. deviation | -- |
share, logshare
|
share within parent, kept at source
|
-- |
No default rule: a value column with neither rule = nor a registered rule is an error.
register_geoscale_rule("capacity",
"sum")
register_geoscale_rule("eff",
"weighted_mean", weight = "pop")
recast_geoscale(x, gs,
target = "country")
clear_geoscale_rules()
register_geoscale_rule(param, rule, weight) pyget_geoscale_rule(), list_geoscale_rules().recast_to_geoleaves(data, gs, source, rule, weight, …) pyweight column.a <- recast_to_geoleaves(y, gs,
source = "country", rule = "sum",
weight = "km2")
recast_from_geoleaves(a, gs,
target = "state", rule = "sum")
state capacity 1 N1 3 2 N2 7 3 S1 20
Leaf rows are keyed by leaf codes: target = another GeoScale with the same leaves.
bands <- geoscale_from_leaftable(
data.frame(band = rep(c("X", "Y"), 3),
cell = x$cell),
geoframes = c("band", "cell"),
name = "bands")
recast_geoscale(x, gs, target = bands,
rule = "sum") |>
recast_geoscale(bands,
target = "band", rule = "sum")
band capacity 1 X 9 2 Y 12
geoscale_map(gs, "state", "zone")
state zone n_from n_overlap w w_from 1 N1 N1 2 2 300 300 2 N2 ZB 2 2 700 700 3 S1 ZB 2 1 500 1100 4 S1 ZC 2 1 600 1100
n_*: leaves; w: overlap weight; w / w_from: the split share.register_geoscale_map(gs, source, target, map, …) pyget_geoscale_map(), clear_geoscale_maps().recast_pairs(data, gs, target, src, dst, rule, directed, …) pylines <- data.frame(
src = c("A1", "A3", "A1"),
dst = c("A2", "A5", "A5"),
capacity = c(100, 200, 300))
recast_pairs(lines, gs,
target = "state", rule = "sum")
src dst capacity 1 N1 S1 300 2 N2 S1 200
A1-A2 lies inside N1; directed = FALSE merges A-B with B-A.data.frame(state = c("N1", "S1")) |>
join_geoscale(gs, geoframes = TRUE)
state example.country example 1 N1 N N1 2 S1 S S1
meta$name, so several GeoScales can join one table; a clash with an existing column is an error. meta = TRUE adds share and weight.The verbs are dplyr pipelines: data.frame, tibble, data.table, dtplyr and arrow return in their own class; lazy inputs return the query unless collect = TRUE.
The generic recast() (nestedscales) dispatches on the scale, so time and space chain:
library(timescales)
d |> # timeslice, state, demand
recast(calendar("m12"),
target = calendar("q4"),
year = 2025, rule = "sum") |>
recast(gs, target = "country",
rule = "sum")gg_geoscale(object, type, data, z, rule, geoframe, view, labels, …)
gg_geoscale(gm, type = "map",
data = recast_geoscale(x, gm,
target = "zone", rule = "sum"),
geoframe = "zone", z = "capacity",
labels = TRUE, alpha = 0.6) +
theme_geoscale()
data, z
|
colour by a value; icicle and stack recast it to coarser geoframes by rule
|
view |
"oblique", "top-down", "isometric", "perspective", "pyramid", ... |
palette |
a viridis letter; NA for one's own scale |
outline |
coarser borders drawn over finer planes |
geoframes |
which to draw; TRUE includes hidden ones |
gg_geoscale(gm, "stack", data = x,
z = "capacity", rule = "sum",
view = "isometric", rotate = 180,
labels = TRUE, alpha = 0.7)
ROW has no value (grey).import pyarrow as pa
import geoscales as gs
s = gs.geoscale_example()
d = pa.table({
"cell": ["A1", "A2", "A3",
"A4", "A5", "A6"],
"cap": [1.0, 2, 3, 4, 5, 6]})
gs.recast_geoscale(d, s,
target="country", rule="sum")
gs.geoscale_id(s) # as in R
- same names and arguments; tables are
pyarrowor the type passed in -
summary()isgeoscale_summary();x@leafisx.leaf -
sfis a GeoDataFrame; a named vector a dict - R only: plots, adjacency, geometry checks
- Without sf loaded,
filter_geoscale()and[turn a stored geometry into a plain list; maps then fail with no applicable method for 'st_union'. Calllibrary(sf)first. -
na_action = "drop"(default) loses the share of uncoded leaves (ROW) with a warning;"keep"adds it as anNArow. - Area is rarely the right weight for demand.
-
prune_geoscale(gs, "zone")stops:ZBlies in two states, and coarser geoframes must nest the new leaves. - Natural Earth at
scale = 110is too coarse for area weights. -
join_geoscale()names columns after the GeoScale and refuses one without aname. - Lazy results (arrow, dtplyr) hold the observed regions only; the full target list appears on collection.