From 3f9b3fd4e237fe76112eb389594ace00ba26e8f3 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 19 Feb 2025 16:37:57 +0100 Subject: [PATCH 1/2] avoid dividing by zero --- R/position-stack.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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)) { From c90b6a34434fcad8a887b509004bf09c8a30ca24 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 19 Feb 2025 16:44:28 +0100 Subject: [PATCH 2/2] add news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 74b049ace5..94644036eb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # ggplot2 (development version) +* `position_fill()` avoids stacking observations of zero (@teunbrand, #6338) * New parameters for `geom_label()` (@teunbrand and @steveharoz, #5365): * The `linewidth` aesthetic is now applied and replaces the `label.size` argument.