Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weighted densities support #90

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ggridges 0.5.6.9000
----------------------------------------------------------------

- Add support for weighted density estimates in `stat_density_ridges()` by
allowing the use of the `weight` aesthetic (@joranE, #90)

ggridges 0.5.6
----------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion R/geoms.R
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ GeomRidgeline <- ggproto("GeomRidgeline", Geom,
#'
#' * **`x`**
#' * **`y`**
#' * `weight` Optional case weights passed to `stats::density` to calculate a weighted density estimate
#' * `group` Defines the grouping. Not needed if a categorical variable is mapped onto `y`, but needed otherwise. Will typically be the same
#' variable as is mapped to `y`.
#' * `height` The height of each ridgeline at the respective x value. Automatically calculated and
Expand Down Expand Up @@ -475,7 +476,7 @@ GeomDensityRidges <- ggproto("GeomDensityRidges", GeomRidgeline,

required_aes = c("x", "y", "height"),

optional_aes = c("point_color", "vline_color", "vline_size", "vline_width"),
optional_aes = c("point_color", "vline_color", "vline_size", "vline_width", "weight"),

extra_params = c("na.rm", "panel_scaling"),

Expand Down
11 changes: 10 additions & 1 deletion R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ stat_density_ridges <- function(mapping = NULL, data = NULL, geom = "density_rid
StatDensityRidges <- ggproto("StatDensityRidges", Stat,
required_aes = "x",

default_aes = aes(height = after_stat(density)),
default_aes = aes(height = after_stat(density), weight = NULL),

dropped_aes = "weight",

calc_panel_params = function(data, params) {
if (is.null(params$bandwidth)) {
Expand Down Expand Up @@ -166,8 +168,15 @@ StatDensityRidges <- ggproto("StatDensityRidges", Stat,
}
panel_id <- as.numeric(panel)

if (is.null(data$weight)) {
weights <- NULL
} else {
weights <- data$weight / sum(data$weight)
}

d <- stats::density(
data$x,
weights = weights,
bw = bandwidth[panel_id], from = from[panel_id], to = to[panel_id], na.rm = TRUE,
n = n
)
Expand Down
1 change: 1 addition & 0 deletions man/geom_density_ridges.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test_stat_density_ridges.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,23 @@ test_that("alternative quantile function can be provided", {
expect_setequal(out$datatype, c("ridgeline", "vline"))
expect_equal(out$x[out$datatype=="vline"], mean(df$x))
})

test_that("unweighted densities are calculated correctly", {
df <- data.frame(x = rnorm(100), wts = runif(100))
df$wts <- df$wts / sum(df$wts)

gg_no_wts <- layer_data(ggplot(df, aes(x = x, y = 0)) + stat_density_ridges())
d_no_wts <- stats::density(df$x)

expect_equal(gg_no_wts$density, d_no_wts$y)
})

test_that("weighted densities are calculated correctly", {
df <- data.frame(x = rnorm(100), wts = runif(100))
df$wts <- df$wts / sum(df$wts)

gg_wts <- layer_data(ggplot(df, aes(x = x, y = 0, weight = wts)) + stat_density_ridges())
d_wts <- stats::density(df$x, weights = df$wts)

expect_equal(gg_wts$density, d_wts$y)
})
Loading