Skip to content

Commit a33953f

Browse files
authored
Fix 6088 (#6106)
* when there is nothing to stack, exit via `var = NULL` * add test
1 parent 842e6be commit a33953f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

R/position-stack.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@ PositionStack <- ggproto("PositionStack", Position,
153153
setup_params = function(self, data) {
154154
flipped_aes <- has_flipped_aes(data)
155155
data <- flip_data(data, flipped_aes)
156+
var <- self$var %||% stack_var(data)
157+
if (!vec_duplicate_any(data$x)) {
158+
# We skip stacking when all data have different x positions so that
159+
# there is nothing to stack
160+
var <- NULL
161+
}
156162
list(
157-
var = self$var %||% stack_var(data),
163+
var = var,
158164
fill = self$fill,
159165
vjust = self$vjust,
160166
reverse = self$reverse,
@@ -185,10 +191,6 @@ PositionStack <- ggproto("PositionStack", Position,
185191
if (is.null(params$var)) {
186192
return(data)
187193
}
188-
if (!vec_duplicate_any(data$x)) {
189-
# Every x is unique, nothing to stack here
190-
return(flip_data(data, params$flipped_aes))
191-
}
192194

193195
negative <- data$ymax < 0
194196
negative[is.na(negative)] <- FALSE

tests/testthat/test-position-stack.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ test_that("negative and positive values are handled separately", {
2525

2626
expect_equal(dat$ymin[dat$x == 2], c(0, -3))
2727
expect_equal(dat$ymax[dat$x == 2], c(2, 0))
28+
29+
# Test only negatives #6088
30+
df <- data_frame0(x = c(1, 1, 2, 2), y = c(-1, -1, -1, -1), g = LETTERS[1:4])
31+
dat <- get_layer_data(ggplot(df, aes(x, y, fill = factor(g))) + geom_col())
32+
expect_equal(dat$ymax, dat$ymin + 1)
33+
expect_equal(dat$ymin, c(-2, -1, -2, -1))
2834
})
2935

3036
test_that("can request reverse stacking", {

0 commit comments

Comments
 (0)