From c70a65f6879871c7b8304f34223bcc25a2ccbd0c Mon Sep 17 00:00:00 2001 From: arthur-mac Date: Thu, 7 Mar 2024 16:23:41 -0300 Subject: [PATCH] ring_sets added --- NAMESPACE | 1 + R/ring_sets.R | 28 ++++++++++++++++++++++++++++ inst/examples/ring_sets.R | 39 +++++++++++++++++++++++++++++++++++++++ man/ring_sets.Rd | 22 ++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 R/ring_sets.R create mode 100644 inst/examples/ring_sets.R create mode 100644 man/ring_sets.Rd diff --git a/NAMESPACE b/NAMESPACE index 7fc7ee1..f375419 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/ring_sets.R b/R/ring_sets.R new file mode 100644 index 0000000..7e3bcf4 --- /dev/null +++ b/R/ring_sets.R @@ -0,0 +1,28 @@ +#' Ring sets +#' @description +#' Divide space in concentric rings and one additional category. +#' @param df A `sf` object. +#' @param cat1 A numeric vector. Ring order, increasing on the polygon size. +#' E.g., the radius for buffers or the time limit for isochrones. +#' @param cat2 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)]) +} diff --git a/inst/examples/ring_sets.R b/inst/examples/ring_sets.R new file mode 100644 index 0000000..0d05b2d --- /dev/null +++ b/inst/examples/ring_sets.R @@ -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))) diff --git a/man/ring_sets.Rd b/man/ring_sets.Rd new file mode 100644 index 0000000..6ed9dce --- /dev/null +++ b/man/ring_sets.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ring_sets.R +\name{ring_sets} +\alias{ring_sets} +\title{Ring sets} +\usage{ +ring_sets(df, cat1, cat2) +} +\arguments{ +\item{df}{A \code{sf} object.} + +\item{cat1}{\if{html}{\out{}} A numeric vector. Ring order, increasing on the polygon size. +E.g., the radius for buffers or the time limit for isochrones.} + +\item{cat2}{\if{html}{\out{}} Second category to guide sets classification.} +} +\value{ +A \code{sf} object +} +\description{ +Divide space in concentric rings and one additional category. +}