Skip to content

Commit ba2e7ed

Browse files
authored
Define non_missing_aes for GeomCol and GeomBar. Closes tidyverse#2715. (tidyverse#2719)
* Define non_missing_aes for GeomCol and GeomBar. Closes tidyverse#2715. * add regression tests * faster unit tests
1 parent 4e9e047 commit ba2e7ed

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

R/geom-bar.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ geom_bar <- function(mapping = NULL, data = NULL,
112112
GeomBar <- ggproto("GeomBar", GeomRect,
113113
required_aes = c("x", "y"),
114114

115+
# These aes columns are created by setup_data(). They need to be listed here so
116+
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
117+
# limits, not just those for which x and y are outside the limits
118+
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),
119+
115120
setup_data = function(data, params) {
116121
data$width <- data$width %||%
117122
params$width %||% (resolution(data$x, FALSE) * 0.9)

R/geom-col.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ geom_col <- function(mapping = NULL, data = NULL,
3232
GeomCol <- ggproto("GeomCol", GeomRect,
3333
required_aes = c("x", "y"),
3434

35+
# These aes columns are created by setup_data(). They need to be listed here so
36+
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
37+
# limits, not just those for which x and y are outside the limits
38+
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),
39+
3540
setup_data = function(data, params) {
3641
data$width <- data$width %||%
3742
params$width %||% (resolution(data$x, FALSE) * 0.9)

tests/testthat/test-geom-bar.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
context("geom_bar")
2+
3+
test_that("geom_bar removes bars with parts outside the plot limits", {
4+
dat <- data.frame(x = c("a", "b", "b", "c", "c", "c"))
5+
6+
p <- ggplot(dat, aes(x)) + geom_bar()
7+
8+
expect_warning( # warning created at render stage
9+
ggplotGrob(p + ylim(0, 2.5)),
10+
"Removed 1 rows containing missing values"
11+
)
12+
})

tests/testthat/test-geom-col.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
context("geom_col")
2+
3+
test_that("geom_col removes columns with parts outside the plot limits", {
4+
dat <- data.frame(x = c(1, 2, 3))
5+
6+
p <- ggplot(dat, aes(x, x)) + geom_col()
7+
8+
expect_warning( # warning created at render stage
9+
ggplotGrob(p + ylim(0.5, 4)),
10+
"Removed 3 rows containing missing values"
11+
)
12+
expect_warning( # warning created at build stage
13+
ggplot_build(p + ylim(0, 2.5)),
14+
"Removed 1 rows containing missing values"
15+
)
16+
})

0 commit comments

Comments
 (0)