|
| 1 | +#' Geometry scale |
| 2 | +#' |
| 3 | +#' The geometry scale is a translation scale, transforming feature identifiers |
| 4 | +#' into geometry using sf objects. |
| 5 | +#' |
| 6 | +#' @param data An sf object with a simple feature list (geometry) column. |
| 7 | +#' @param id A string giving a column name in `data` to use as feature identifiers. |
| 8 | +#' @param ... Passed on to [discrete_scale]. |
| 9 | +#' |
| 10 | +#' @export |
| 11 | +#' @include scale-identity.R |
| 12 | +#' |
| 13 | +#' @examplesIf require("rnaturalearth") |
| 14 | +#' world <- rnaturalearth::ne_countries() |
| 15 | +#' |
| 16 | +#' # 1:1 mapping using three-letter country codes |
| 17 | +#' iso_a3 <- data.frame(id = c("AUS", "NZL", "PNG", "IDN")) |
| 18 | +#' |
| 19 | +#' ggplot(iso_a3) + |
| 20 | +#' geom_sf(aes(geometry = id, fill = id)) + |
| 21 | +#' scale_geometry_discrete(world, "iso_a3") |
| 22 | +#' |
| 23 | +#' # 1:many mapping using regions |
| 24 | +#' regions <- data.frame(id = c("Asia", "Europe")) |
| 25 | +#' |
| 26 | +#' ggplot(regions) + |
| 27 | +#' geom_sf(aes(geometry = id, fill = id)) + |
| 28 | +#' scale_geometry_discrete(world, "region_un") |
| 29 | +#' |
| 30 | +#' # Subnational identities |
| 31 | +#' states <- rnaturalearth::ne_states(country = "united states of america") |
| 32 | +#' |
| 33 | +#' west_us <- data.frame(id = c("WA", "OR", "CA")) |
| 34 | +#' |
| 35 | +#' ggplot(west_us) + |
| 36 | +#' geom_sf(aes(geometry = id, fill = id)) + |
| 37 | +#' scale_geometry_discrete(states, "postal") |
1 | 38 | scale_geometry_discrete <- function(data = NULL, id = NULL, ...) { |
2 | 39 | check_installed("sf") |
3 | 40 | transform <- new_geometry_transform(data, id) |
|
0 commit comments