Skip to contents

A GeoScale holds leaf regions and the geoframes that group them, with weights, labels and optional geometry; data is recast between geoframes through the leaves

PDF, A4, 2 pages · geoscales 0.8.0 · 2026-11-29
A GeoScale
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
One row per leaf, one column per geoframe (coarsest first), then the weights; 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.

Drawn
Vocabulary
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.

Build from a table
geoscale_from_leaftable(leaftable, geoframes, leaf, weights, name, …) py
The general constructor.
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")
Numeric columns become weights; "" in a geoframe column reads as NA; rows are sorted by geoframe unless row_order = "given".
geoscale_build(..., geoframes, weights, name) py
From two-column parent-child crosswalks; a cross-cutting geoframe is a crosswalk against the finest one.
Providers, Natural Earth

No maps are bundled: a provider fetches a wide table whose columns become geoframes and weights.

ne_geoscale(scale, geoframes, weights, …) py
continent > subregion > country > feature, weights pop_est, gdp_md.
ne_source(scale, geoframe, country, …) py
The sf table: "country" (admin-0) or "states" (admin-1, needs rnaturalearthhires).
geoscale_from_provider(provider, geoframes, weights, name, …) py
A registered provider, or an sf 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")
Inspect
geoscale_geoframes(x, finest, visible) py
Coarsest first; finest = TRUE: the leaf geoframe.
geoscale_regions(x, geoframe) py
The members of a geoframe, in order.
geoscale_leaftable(x) py
Also as.data.frame(gs).
geoscale_rank(x, geoframe) py
Coarsest = 1.
geoscale_id(gs) py
"name@hash": the hash covers geoframes and memberships, not weights, labels or geometry.
geoscale_tree(x) py
Each part marked fixed, derived or mutable.

summary(gs): weight totals, coverage, nesting, geometry. gs@meta, gs@members: the S7 properties.

Navigate

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_ancestry(x) py
All (parent, child) pairs.
geoscale_family(x, parent, child) py
Immediate parent-child table.
geoscale_nests(x, parent, child) py
FALSE for state/zone, with "ZB" in attr(, "offenders").
geoscale_share(x, geoframe, weight, within) py
Weights as shares of the whole, or of each within region.
Weights, labels, hidden
geoscale_add_weight(x, ..., default) py
Another weight: a vector named by leaf code, or a table keyed by the leaf column.
attach_labels_geoscale(x, labels, variant, …) py
Display names from a table geoframe, region, label.
gl <- 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) py
The table; geoscale_label() returns the code where no label exists.
hide_geoframes_geoscale(x, geoframes) py
Out of plots only: recasts, joins, navigation and geoscale_id() unchanged. show_geoframes_geoscale() undoes it.
Subset, coarsen, zoom
filter_geoscale(x, geoframe, region, …) py
Keep the leaves of some regions; also gs["country", "N"].
n <- filter_geoscale(gs, "country", "N")
geoscale_coverage(n)
      km2       pop 
0.3225806 0.6666667 
A subset is a sample: named example[country:N], with the kept share of the root's weight totals.
geoscale_coverage(x, weight) py
prune_geoscale(x, geoframe, keep_geometry) py
That geoframe becomes the leaf layer: finer ones go, weights are summed, shapes dissolved; named example@state.
zoom_geoscale(x, focus, geoframes, name, …) py
Focus leaves kept, the rest in rings of coarser regions.
z <- zoom_geoscale(gs, focus = "A1",
  geoframes = c("country", "state"))
geoscale_regions(z, "zoom")
[1] "A1"      "N1_rest" "N_rest"  "S"      
Geoframes of z: country, zoom, cell.
Geometry

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) py
An sf is matched on its code column by. A bare sfc binds by position, refused when the constructor re-sorted the rows unless positional = TRUE.
geoscale_geometry(x, geoframe, …) py
Leaves dissolved to a geoframe, as sf.
add_area_geoscale(x, name, crs) py
Area weight in an equal-area CRS.
check_geometry_geoscale(x, reference, by, …)
Is each shape on its own row? Proof with a reference, evidence without.
coords_to_region(x, gs, geoframe, coords, crs) py
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, …)
Neighbour matrix, "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 between geoframes
recast_geoscale(data, gs, source, target, values, rule, weight, na_action, …) py
Up, down and across, routed through the leaves.
x <- 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
Down, 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
An intensity: weighted mean going up, copied going down.

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 --
geoscale_rule_action(rule) py
This table, with divisible and action. All names: GEOSCALE_RULES.
Rule registry

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) py
Per column name, for the session; also get_geoscale_rule(), list_geoscale_rules().
Leaves, and two GeoScales
recast_to_geoleaves(data, gs, source, rule, weight, …) py
Down to one row per leaf, with a weight column.
recast_from_geoleaves(data, gs, target, rule, data_weight, …) py
Leaf rows up to a geoframe.
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_between(source, target, weight, …) py
Crosswalks
geoscale_map(gs, source, target, weight, …) py
The recast route as a table.
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, …) py
An exact concordance used instead; also get_geoscale_map(), clear_geoscale_maps().
Region pairs: flows, lines
recast_pairs(data, gs, target, src, dst, rule, directed, …) py
Both endpoints mapped; pairs inside one target region dropped. Aggregation only.
lines <- 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.
Join to data
join_geoscale(data, gs, geoframe, geoframes, meta, …) py
Decorates, does not convert.
data.frame(state = c("N1", "S1")) |>
  join_geoscale(gs, geoframes = TRUE)
  state example.country example
1    N1               N      N1
2    S1               S      S1
Columns named after meta$name, so several GeoScales can join one table; a clash with an existing column is an error. meta = TRUE adds share and weight.
Backends, time and space

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")
Plots
gg_geoscale(object, type, data, z, rule, geoframe, view, labels, …)
"icicle", "stack", "map"; autoplot() returns it, plot() draws it.
Choropleth of capacity by zone on the example squares
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
geom_geoscale(gs, z, geoframe, fun, …)
A geom_sf() layer for any ggplot(); column names, not aes().
geoscale_layout(x, weight, …)
The icicle as a data.frame.
Data on every plane
Isometric stack coloured by capacity summed to every geoframe
gg_geoscale(gm, "stack", data = x,
  z = "capacity", rule = "sum",
  view = "isometric", rotate = 180,
  labels = TRUE, alpha = 0.7)
Cell values summed up to every coarser plane; ROW has no value (grey).
Python
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 pyarrow or the type passed in
  • summary() is geoscale_summary(); x@leaf is x.leaf
  • sf is a GeoDataFrame; a named vector a dict
  • R only: plots, adjacency, geometry checks
Pitfalls
  • Without sf loaded, filter_geoscale() and [ turn a stored geometry into a plain list; maps then fail with no applicable method for 'st_union'. Call library(sf) first.
  • na_action = "drop" (default) loses the share of uncoded leaves (ROW) with a warning; "keep" adds it as an NA row.
  • Area is rarely the right weight for demand.
  • prune_geoscale(gs, "zone") stops: ZB lies in two states, and coarser geoframes must nest the new leaves.
  • Natural Earth at scale = 110 is too coarse for area weights.
  • join_geoscale() names columns after the GeoScale and refuses one without a name.
  • Lazy results (arrow, dtplyr) hold the observed regions only; the full target list appears on collection.