diff --git a/NEWS.md b/NEWS.md index 0fd950dcf6..6e0540dd85 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,8 +6,8 @@ (@mitchelloharawild, #6609). * Fixed regression where `scale_{x,y}_*()` threw an error when an expression object is set to `labels` argument (@yutannihilation, #6617). - - +* `stat_ydensity()` now only requires the `x` or `y` aesthetic. The other will + be populated with 0, similar to `stat_boxplot()` (@teunbrand, #6600) * Allow `stat` in `geom_hline`, `geom_vline`, and `geom_abline`. (@sierrajohnson, #6559) # ggplot2 4.0.0 diff --git a/R/stat-ydensity.R b/R/stat-ydensity.R index 969723ebd4..01a862cdc1 100644 --- a/R/stat-ydensity.R +++ b/R/stat-ydensity.R @@ -4,7 +4,7 @@ #' @export StatYdensity <- ggproto( "StatYdensity", Stat, - required_aes = c("x", "y"), + required_aes = "x|y", non_missing_aes = "weight", setup_params = function(data, params) { @@ -23,6 +23,12 @@ StatYdensity <- ggproto( params }, + setup_data = function(self, data, params) { + var <- flipped_names(flip = params$flipped_aes)$x + data[[var]] <- data[[var]] %||% 0 + data + }, + # `draw_quantiles` is here for deprecation repair reasons extra_params = c("na.rm", "orientation", "draw_quantiles"),