diff --git a/NEWS.md b/NEWS.md index 2c2e95a021..829d029bb5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `stat_ydensity()` now only requires the `x` or `y` aesthetic. The other will + be populated with 0, similar to `stat_boxplot()` (@teunbrand, #6600) * Implemented `as.list()` and `S7::convert()` methods for lists and classes in ggplot2 (@teunbrand, #6695) * The default linetype in `geom_sf()` is derived from `geom_polygon()` for diff --git a/R/stat-ydensity.R b/R/stat-ydensity.R index 7698601dcc..750343eee8 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"),