Skip to content

Commit

Permalink
ring_sets added
Browse files Browse the repository at this point in the history
  • Loading branch information
baarthur committed Mar 7, 2024
1 parent fefca78 commit c70a65f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(gg_rqs)
export(maphub_tidy_lines)
export(maphub_tidy_stations)
export(maphub_to_sf)
export(ring_sets)
export(shp_extract_read)
export(summarise_set)
export(theme_abnq)
Expand Down
28 changes: 28 additions & 0 deletions R/ring_sets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Ring sets
#' @description
#' Divide space in concentric rings and one additional category.
#' @param df A `sf` object.
#' @param cat1 <data-masking> A numeric vector. Ring order, increasing on the polygon size.
#' E.g., the radius for buffers or the time limit for isochrones.
#' @param cat2 <data-masking> Second category to guide sets classification.
#' @export
#' @returns A `sf` object



# function ------------------------------------------------------------------------------------

ring_sets <- function(df, cat1, cat2) {
levels <- sort(unique(dplyr::pull(df, {{cat1}})))

rings <- rev(seq(levels)) %>%
purrr::map(
\(x) summarise_set(df, {{cat1}}, {{cat2}}, order = x)
)

rings <- purrr::map2(
rings[-length(rings)], rings[-1],
\(x, y) trim_set(y, x, {{cat1}})
) %>%
dplyr::bind_rows(rings[length(rings)])
}
39 changes: 39 additions & 0 deletions inst/examples/ring_sets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
library(magrittr)

# Generate data
pts <- sf::st_sfc(
sf::st_point(c(0,0)),
sf::st_point(c(1,1)),
sf::st_point(c(1,-1)),
sf::st_point(c(-1,1)),
sf::st_point(c(-1,-1))
) %>%
sf::st_as_sf() %>%
cbind(inaug = c(1970, 1980, 1990, 2000, 2010),
status = c("Always treated", "Always treated", "Treated", "Treated", "Never treated")) %>%
dplyr::rename(geometry = x)

df <- seq(0.25, 1.25, 0.25) %>%
purrr::map(
\(x) sf::st_buffer(pts, x) %>%
dplyr::mutate(rad = x)
) %>%
dplyr::bind_rows()


# Make ring sets
rings <- ring_sets(df, rad, inaug)


# Dataviz

## ggplot2
# rings %>%
# ggplot2::ggplot() +
# ggplot2::geom_sf(ggplot2::aes(fill = factor(rad)), color = NA, alpha = 0.5) +
# ggplot2::scale_fill_viridis_d() +
# ggplot2::theme_void()

## base plot
plot(rings$geometry, col = factor(rings$rad), border = "transparent", axes = F)
legend("topright", legend = unique(rings$rad), fill = factor(unique(rings$rad)))
22 changes: 22 additions & 0 deletions man/ring_sets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c70a65f

Please sign in to comment.