Skip to content

POC: Geometry scale - #6896

Draft
teunbrand wants to merge 2 commits into
mainfrom
poc_geometry_scale
Draft

POC: Geometry scale#6896
teunbrand wants to merge 2 commits into
mainfrom
poc_geometry_scale

Conversation

@teunbrand

Copy link
Copy Markdown
Collaborator

This PR is merely a POC of what a geometry scale could look like.
It is 'bring your own spatial data', but the rest is done just via identifiers from that data.

The scale works by having the translation from identifiers to geometry at the scale transformation level.
Scale transformations play out before stat computations, so stat_sf() has the correct data to determine the bounding box.
There are no guides or other reasons to ever inverse-transform the data, so I think this is somewhat fine.

It currently has two big limitations:

  • The translated geometry doesn't obey coord_sf(crs). For normal geom_sf(), this happens in the CoordSf$setup_data() step, which runs before scale transformation, so the translated geometry misses out on that step. It can maybe be moved to stat_sf(), but that'd be somewhat more invasive.
  • The scale_type() mechanism to automatically detect scales isn't rigged to detect non-sfc class geometry aesthetic. The detection of missing scales runs after scale transformation takes place, so even if we rig up scale_type() correctly, the order of orchestration still gets in the way.

Here are the rendered examples

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
world <- rnaturalearth::ne_countries()

# 1:1 mapping using three-letter country codes
iso_a3 <- data.frame(id = c("AUS", "NZL", "PNG", "IDN"))

ggplot(iso_a3) +
  geom_sf(aes(geometry = id, fill = id)) +
  scale_geometry_discrete(world, "iso_a3")

# 1:many mapping using regions
regions <- data.frame(id = c("Asia", "Europe"))

ggplot(regions) +
  geom_sf(aes(geometry = id, fill = id)) +
  scale_geometry_discrete(world, "region_un")

# Subnational identities
states <- rnaturalearth::ne_states(country = "united states of america")

west_us <- data.frame(id = c("WA", "OR", "CA"))

ggplot(west_us) +
  geom_sf(aes(geometry = id, fill = id)) +
  scale_geometry_discrete(states, "postal")

Created on 2026-09-06 with reprex v2.1.1

@teunbrand
teunbrand marked this pull request as draft September 6, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant