Skip to content

Commit ec58f51

Browse files
authored
Fix for position_stack(vjust) (#6694)
* update skipping conditions * cover test case * add news bullet
1 parent 458bf80 commit ec58f51

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* Fixed regression where `position_stack(vjust)` was ignored when there are
4+
only single groups (#6692)
35
* Fixed bug where `NA` handling in `geom_path()` was ignoring panels (@teunbrand, #6533)
46
* Logical values for the linetype aesthetic will be interpreted numerically,
57
so that `linetype = FALSE` becomes 0/'blank' and `linetype = TRUE` becomes

R/position-stack.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ PositionStack <- ggproto("PositionStack", Position,
154154
flipped_aes <- has_flipped_aes(data)
155155
data <- flip_data(data, flipped_aes)
156156
var <- self$var %||% stack_var(data)
157-
if (!vec_duplicate_any(data$x) && !isTRUE(self$fill)) {
157+
if (!vec_duplicate_any(data$x) &&
158+
!isTRUE(self$fill) &&
159+
all(self$vjust == 1)) {
158160
# We skip stacking when all data have different x positions so that
159161
# there is nothing to stack
160162
var <- NULL

tests/testthat/test-position-stack.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ test_that("data with no extent is stacked correctly", {
5050
y = c(-40, -75),
5151
group = letters[1:2]
5252
)
53-
base <- ggplot(df, aes(x, y, group = group))
54-
p0 <- base + geom_text(aes(label = y), position = position_stack(vjust = 0))
55-
p1 <- base + geom_text(aes(label = y), position = position_stack(vjust = 1))
53+
base <- ggplot(df, aes(x, y, group = group, label = y))
54+
p0 <- base + geom_text(position = position_stack(vjust = 0))
55+
p1 <- base + geom_text(position = position_stack(vjust = 1))
56+
p2 <- base + geom_text(aes(group = 1L), position = position_stack(vjust = 0.5))
5657

5758
expect_equal(get_layer_data(p0)$y, c(-115, -75))
5859
expect_equal(get_layer_data(p1)$y, c(-75, 0))
60+
expect_equal(get_layer_data(p2)$y, c(df$y[1] / 2, df$y[1] + df$y[2] / 2))
5961
})
6062

6163
test_that("position_stack() can stack correctly when ymax is NA", {

0 commit comments

Comments
 (0)