Skip to content

Commit

Permalink
Fix 6088 (#6106)
Browse files Browse the repository at this point in the history
* when there is nothing to stack, exit via `var = NULL`

* add test
  • Loading branch information
teunbrand authored Sep 23, 2024
1 parent 842e6be commit a33953f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 7 additions & 5 deletions R/position-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,14 @@ PositionStack <- ggproto("PositionStack", Position,
setup_params = function(self, data) {
flipped_aes <- has_flipped_aes(data)
data <- flip_data(data, flipped_aes)
var <- self$var %||% stack_var(data)
if (!vec_duplicate_any(data$x)) {
# We skip stacking when all data have different x positions so that
# there is nothing to stack
var <- NULL
}
list(
var = self$var %||% stack_var(data),
var = var,
fill = self$fill,
vjust = self$vjust,
reverse = self$reverse,
Expand Down Expand Up @@ -185,10 +191,6 @@ PositionStack <- ggproto("PositionStack", Position,
if (is.null(params$var)) {
return(data)
}
if (!vec_duplicate_any(data$x)) {
# Every x is unique, nothing to stack here
return(flip_data(data, params$flipped_aes))
}

negative <- data$ymax < 0
negative[is.na(negative)] <- FALSE
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-position-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ test_that("negative and positive values are handled separately", {

expect_equal(dat$ymin[dat$x == 2], c(0, -3))
expect_equal(dat$ymax[dat$x == 2], c(2, 0))

# Test only negatives #6088
df <- data_frame0(x = c(1, 1, 2, 2), y = c(-1, -1, -1, -1), g = LETTERS[1:4])
dat <- get_layer_data(ggplot(df, aes(x, y, fill = factor(g))) + geom_col())
expect_equal(dat$ymax, dat$ymin + 1)
expect_equal(dat$ymin, c(-2, -1, -2, -1))
})

test_that("can request reverse stacking", {
Expand Down

0 comments on commit a33953f

Please sign in to comment.