Skip to content

Define non_missing_aes for GeomCol and GeomBar. Closes #2715. #2719

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

Merged
merged 3 commits into from
Jul 9, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions R/geom-bar.r
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ geom_bar <- function(mapping = NULL, data = NULL,
GeomBar <- ggproto("GeomBar", GeomRect,
required_aes = c("x", "y"),

# These aes columns are created by setup_data(). They need to be listed here so
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
# limits, not just those for which x and y are outside the limits
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),

setup_data = function(data, params) {
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
Expand Down
5 changes: 5 additions & 0 deletions R/geom-col.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ geom_col <- function(mapping = NULL, data = NULL,
GeomCol <- ggproto("GeomCol", GeomRect,
required_aes = c("x", "y"),

# These aes columns are created by setup_data(). They need to be listed here so
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
# limits, not just those for which x and y are outside the limits
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),

setup_data = function(data, params) {
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-geom-bar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context("geom_bar")

test_that("geom_bar removes bars with parts outside the plot limits", {
dat <- data.frame(x = c("a", "b", "b", "c", "c", "c"))

p <- ggplot(dat, aes(x)) + geom_bar()

expect_warning( # warning created at render stage
ggplotGrob(p + ylim(0, 2.5)),
"Removed 1 rows containing missing values"
)
})
16 changes: 16 additions & 0 deletions tests/testthat/test-geom-col.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
context("geom_col")

test_that("geom_col removes columns with parts outside the plot limits", {
dat <- data.frame(x = c(1, 2, 3))

p <- ggplot(dat, aes(x, x)) + geom_col()

expect_warning( # warning created at render stage
ggplotGrob(p + ylim(0.5, 4)),
"Removed 3 rows containing missing values"
)
expect_warning( # warning created at build stage
ggplot_build(p + ylim(0, 2.5)),
"Removed 1 rows containing missing values"
)
})