Skip to content

Commit 00eedd1

Browse files
committed
Fixes #1691. Positive and negative y-values are now calculated separately to allow stacking below the x-axis
1 parent 6d41ddd commit 00eedd1

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

R/position-stack.r

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,26 @@ PositionStack <- ggproto("PositionStack", Position,
6262
return(data)
6363
}
6464

65-
if (!is.null(data$ymin) && !all(data$ymin == 0))
66-
warning("Stacking not well defined when ymin != 0", call. = FALSE)
65+
if (!is.null(data$ymin) && !all((data$ymin == 0 & data$ymax >= 0) | data$ymax == 0 & data$ymin <= 0))
66+
warning("Stacking not well defined when ymin and ymax is on opposite sides of 0", call. = FALSE)
6767

6868
data
6969
},
7070

7171
compute_panel = function(data, params, scales) {
72-
collide(data, NULL, "position_stack", pos_stack)
72+
negative <- if (!is.null(data$ymin)) data$ymin < 0 else rep(FALSE, nrow(data))
73+
neg <- data[which(negative), ]
74+
pos <- data[which(!negative), ]
75+
if (any(negative)) {
76+
# Negate group so sorting order is consistent across the x-axis.
77+
# Undo negation afterwards so it doesn't mess up the rest
78+
neg$group <- -neg$group
79+
neg <- collide(neg, NULL, "position_stack", pos_stack)
80+
neg$group <- -neg$group
81+
}
82+
if (any(!negative)) {
83+
pos <- collide(pos, NULL, "position_stack", pos_stack)
84+
}
85+
rbind(pos, neg)
7386
}
7487
)

0 commit comments

Comments
 (0)