Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip alignment/stacking when possible #5789

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
per layer (@teunbrand, #5740).
* The `fill` of the `panel.border` theme setting is ignored and forced to be
transparent (#5782).
* `stat_align()` skips computation when there is only 1 group and therefore
alignment is not necessary (#5788).
* `position_stack()` skips computation when all `x` values are unique and
therefore stacking is not necessary (#5788).

# ggplot2 3.5.1

Expand Down
4 changes: 4 additions & 0 deletions R/position-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ 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
21 changes: 9 additions & 12 deletions R/stat-align.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,19 @@ StatAlign <- ggproto("StatAlign", Stat,
if (empty(data)) {
return(data_frame0())
}

if (is_unique(data$group)) {
return(data)
}
names <- flipped_names(flipped_aes)
x <- data[[names$x]]
y <- data[[names$y]]

if (is_unique(data$group)) {
# No need for interpolation
cross <- x[0]
} else {
# Find positions where 0 is crossed
pivot <- vec_unrep(data_frame0(group = data$group, y = y < 0))
group_ends <- cumsum(vec_unrep(pivot$key$group)$times)
pivot <- cumsum(pivot$times)[-group_ends]
cross <- -y[pivot] * (x[pivot + 1] - x[pivot]) /
(y[pivot + 1] - y[pivot]) + x[pivot]
}
# Find positions where 0 is crossed
pivot <- vec_unrep(data_frame0(group = data$group, y = y < 0))
group_ends <- cumsum(vec_unrep(pivot$key$group)$times)
pivot <- cumsum(pivot$times)[-group_ends]
cross <- -y[pivot] * (x[pivot + 1] - x[pivot]) /
(y[pivot + 1] - y[pivot]) + x[pivot]

unique_loc <- unique(sort(c(x, cross)))
adjust <- diff(range(unique_loc, na.rm = TRUE)) * 0.001
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-stat-align.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,20 @@ test_that("alignment adjusts per panel", {
# data into account (#5227)

df <- data_frame0(
x = c(0, 1, 1000, 1001),
y = c(-1, 1, -1, 1),
g = c("A", "A", "B", "B")
x = c(0, 1, 1000, 1001, 0, 1, 1000, 1001),
y = c(-1, 1, -1, 1, -1, 1, -1, 1),
f = c("A", "A", "B", "B", "A", "A", "B", "B"),
g = c("a", "a", "b", "b", "c", "c", "d", "d")
Comment on lines +51 to +54
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to ensure >=2 groups per panel here for this test to test the thing it is supposed to test.

)
p <- ggplot(df, aes(x, y))
p <- ggplot(df, aes(x, y, group = g))

# Here, x-range is large, so adjustment should be larger
ld <- layer_data(p + geom_area(aes(fill = g)))
ld <- layer_data(p + geom_area(aes(fill = f)))
expect_equal(diff(ld$x[1:2]), 1/6, tolerance = 1e-4)

# Here, x-ranges are smaller, so adjustment should be smaller instead of
# considering the data as a whole
ld <- layer_data(p + geom_area() + facet_wrap(vars(g), scales = "free_x"))
ld <- layer_data(p + geom_area() + facet_wrap(vars(f), scales = "free_x"))
expect_equal(diff(ld$x[1:2]), 1e-3, tolerance = 1e-4)

})