Skip to content

Commit

Permalink
Merge pull request #30 from JoFrhwld/29-re-write-examples-and-change-…
Browse files Browse the repository at this point in the history
…defaults-to-assume-dplyrreframe

29 re write examples and change defaults to assume dplyr reframe
  • Loading branch information
JoFrhwld authored Sep 28, 2023
2 parents bddb612 + 9dcf19c commit 7d3822b
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 355 deletions.
53 changes: 39 additions & 14 deletions R/density_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ sf_polygon_safely <- function(...){
isolines_to_df <- function(isolines, probs, nameswap){
isolines |>
dplyr::mutate(
line_id = .data$line |>
level_id = .data$line |>
as.numeric() |>
dplyr::desc() |>
as.factor() |>
as.numeric(),
prob = sort(probs)[.data$line_id],
as.integer(),
prob = sort(probs)[.data$level_id],
order = dplyr::row_number()
) |>
dplyr::select(-"line") |>
dplyr::select("line_id",
dplyr::select("level_id",
"id",
"prob",
"x",
Expand All @@ -106,7 +106,7 @@ iso_df_to_sf <- function(iso_poly_df, xname, yname){

iso_poly_df |>
dplyr::mutate(
polygon_id = paste(.data$line_id, .data$id, sep = "-")
polygon_id = paste(.data$level_id, .data$id, sep = "-")
) -> df_prepared

df_prepared |>
Expand All @@ -128,7 +128,7 @@ iso_df_to_sf <- function(iso_poly_df, xname, yname){

iso_poly_pieces_sf |>
dplyr::group_by(
.data$line_id, .data$prob
.data$level_id, .data$prob
) |>
dplyr::summarise() ->
iso_poly_sf
Expand All @@ -145,23 +145,48 @@ iso_df_to_sf <- function(iso_poly_df, xname, yname){
#' densities.
#'
#' @details
#' When using `density_polygons()` together with tidyverse verbs, like
#' [dplyr::summarise()], `as_list` should be `TRUE`.
#' When using `density_polygons()` together with [dplyr::summarise()], `as_list`
#' should be `TRUE`.
#'
#'
#' @param x,y Numeric data dimensions
#' @param probs Probabilities to compute density polygons for
#' @param as_sf Should the returned values be [sf::sf]? Defaults to `FALSE`.
#' @param as_list Should the returned value be a list? Defaults to `TRUE` to
#' work well with tidyverse list columns
#' @param as_list Should the returned value be a list? Defaults to `FALSE` to
#' work with [dplyr::reframe()]
#' @param range_mult A multiplier to the range of `x` and `y` across which the
#' probability density will be estimated.
#' @param rangex,rangey Custom ranges across `x` and `y` ranges across which the
#' probability density will be estimated.
#' @param ... Additional arguments to be passed to [ggdensity::get_hdr()]
#'
#' @returns A list of data frames, if `as_list=TRUE`, or just a data frame,
#' if `as_list=FALSE`
#' if `as_list=FALSE`.
#'
#' ## Data frame output
#'
#' If `as_sf=FALSE`, the data frame has the following columns:
#' \describe{
#' \item{level_id}{An integer id for each probability level}
#' \item{id}{An integer id for each sub-polygon within a probabilty level}
#' \item{prob}{The probability level (originally passed to `probs`)}
#' \item{x, y}{The values along the original `x` and `y` dimensions defining
#' the density polygon. These will be renamed to the original input variable
#' names.}
#' \item{order}{The original plotting order of the polygon points, for
#' convenience.}
#' }
#'
#' ## sf output
#' If `as_sf=TRUE`, the data frame has the following columns:
#' \describe{
#' \item{level_id}{An integer id for each probability level}
#' \item{prob}{The probability level (originally passed to `probs`)}
#' \item{geometry}{A column of [sf::st_polygon()]s.}
#' }
#'
#' This output will need to be passed to [sf::st_sf()] to utilize many of the
#' features of [sf].
#'
#' @details
#' If both `rangex` and `rangey` are defined, `range_mult` will be disregarded.
Expand All @@ -180,7 +205,7 @@ density_polygons <- function(x,
y,
probs = 0.5,
as_sf = FALSE,
as_list = TRUE,
as_list = FALSE,
range_mult = 0.25,
rangex = NULL,
rangey = NULL,
Expand Down Expand Up @@ -273,8 +298,8 @@ density_polygons <- function(x,
density_area <- function(x,
y,
probs = 0.5,
as_sf = F,
as_list = T,
as_sf = FALSE,
as_list = FALSE,
range_mult = 0.25,
rangex = NULL,
rangey = NULL,
Expand Down
66 changes: 25 additions & 41 deletions inst/examples/density_area_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,16 @@ y <- rnorm(100)

density_area(x,
y,
probs = ppoints(50),
as_list = FALSE) ->
probs = ppoints(50)) ->
poly_areas_df

head(poly_areas_df)

# Plotting the relationship between probability level and area
ggplot(poly_areas_df,
aes(prob, area))+
aes(prob, area)) +
geom_line()

# Assuming distribution is circular, the radius would be `sqrt(area/pi).`
poly_areas_df |>
mutate(
radius = sqrt(area/pi)
) |>
ggplot(aes(prob, radius)) +
geom_line()

# Tidyverse usage

data(s01)
Expand All @@ -39,41 +30,34 @@ data(s01)

s01 |>
mutate(log_F2 = -log(F2),
log_F1 = -log(F1))->
log_F1 = -log(F1)) ->
s01

### Data frame output

s01 |>
group_by(name) |>
summarise(
area_df = density_area(log_F2,
log_F1,
probs = ppoints(10),
as_sf = FALSE,
n = 200),
area_sf = density_area(log_F2,
log_F1,
probs = ppoints(10),
as_sf = TRUE,
n = 200)
) ->
s01_areas
reframe(density_area(log_F2,
log_F1,
probs = ppoints(10))) ->
s01_areas_df

s01_areas |>
unnest(area_df) |>
ggplot(
aes(prob, area)
)+
geom_line()
s01_areas_df |>
ggplot(aes(prob, area)) +
geom_line()

# If the shape were an equilateral triangle, each side would be
# equal to `sqrt(area * (4/sqrt(3)))`
### Including sf output

s01_areas |>
unnest(
area_sf
) |>
st_sf() |>
s01 |>
group_by(name) |>
reframe(density_area(log_F2,
log_F1,
probs = ppoints(10),
as_sf = TRUE)) |>
st_sf() ->
s01_areas_sf

s01_areas_sf |>
arrange(desc(prob)) |>
mutate(side = sqrt(area * (4/sqrt(3)))) |>
ggplot()+
geom_sf(aes(fill = side))
ggplot() +
geom_sf(aes(fill = area))
Loading

0 comments on commit 7d3822b

Please sign in to comment.