diff --git a/NEWS.md b/NEWS.md index 823adbcfa8..1673d86832 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # ggplot2 (development version) +* `position_fill()` avoids stacking observations of zero (@teunbrand, #6338) * New `layer(layout)` argument to interact with facets (@teunbrand, #3062) * New `stat_connect()` to connect points via steps or other shapes (@teunbrand, #6228) diff --git a/R/position-stack.R b/R/position-stack.R index de23456625..0e35f1191e 100644 --- a/R/position-stack.R +++ b/R/position-stack.R @@ -224,7 +224,10 @@ pos_stack <- function(df, width, vjust = 1, fill = FALSE) { heights <- c(0, cumsum(y)) if (fill) { - heights <- heights / abs(heights[length(heights)]) + total <- abs(heights[length(heights)]) + if (total > sqrt(.Machine$double.eps)) { + heights <- heights / total + } } # We need to preserve ymin/ymax order. If ymax is lower than ymin in input, it should remain that way if (!is.null(df$ymin) && !is.null(df$ymax)) {