Skip to content

Commit 367ade9

Browse files
authored
Silently drop out-of-bounds padding in stat_align() (#6673)
* Remove oob-padding silently * add news bullet * add test
1 parent f147aaf commit 367ade9

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Bug fixes
44

5+
* Out-of-bounds datapoints used as padding by `stat_align()` now get removed
6+
silently rather than verbosely (@teunbrand, #6667)
57
* Fixed bug where `stat_bin(boundary)` was ignored (#6682).
68
* `geom_text()` and `geom_label()` accept expressions as the `label` aesthetic
79
(@teunbrand, #6638)

R/stat-align.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ StatAlign <- ggproto(
7373
flipped_aes = flipped_aes
7474
)
7575
flip_data(data_aligned, flipped_aes)
76+
},
77+
78+
finish_layer = function(data, params) {
79+
# Silently remove out-of-bounds padding vertices
80+
var <- flipped_names(params$flipped_aes)$x
81+
remove <- is.na(data[[var]]) & (data$align_padding %||% FALSE)
82+
vec_slice(data, !remove)
7683
}
7784
)
7885

tests/testthat/test-stat-align.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,24 @@ test_that("alignment adjusts per panel", {
5050
expect_equal(diff(ld$x[1:2]), 1e-3, tolerance = 1e-4)
5151

5252
})
53+
54+
test_that("out-of-bounds padding is removed (#6667)", {
55+
df <- data_frame0(
56+
g = rep(c("a", "b"), each = 3L),
57+
x = c(1, 3, 5, 2, 4, 6),
58+
y = c(2, 5, 1, 3, 6, 7)
59+
)
60+
p <- ggplot(df, aes(x, y, fill = g)) + geom_area()
61+
ld <- layer_data(p)
62+
expect_equal(sum(ld$align_padding), 4)
63+
# The first and last datapoints should be padding
64+
expect_equal(ld$align_padding[c(1, nrow(ld))], c(TRUE, TRUE))
65+
66+
67+
ld <- layer_data(
68+
p + scale_x_continuous(limits = range(df$x), expand = c(0, 0))
69+
)
70+
expect_equal(sum(ld$align_padding), 2)
71+
# The first and last datapoints should not be padding
72+
expect_equal(ld$align_padding[c(1, nrow(ld))], c(FALSE, FALSE))
73+
})

0 commit comments

Comments
 (0)