diff --git a/NEWS.md b/NEWS.md index 96b0f65b80..dbac0a777b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* `position_stack()` no longer silently removes missing data, which is now + handled by the geom instead of position (#3532). + * Legend keys that can draw arrows have their size adjusted for arrows. * The `trans` argument in scales and secondary axes has been renamed to diff --git a/R/position-stack.R b/R/position-stack.R index 2aacb638bb..ac445be071 100644 --- a/R/position-stack.R +++ b/R/position-stack.R @@ -175,11 +175,10 @@ PositionStack <- ggproto("PositionStack", Position, ymax = as.numeric(ifelse(data$ymax == 0, data$ymin, data$ymax)) ) - data <- remove_missing( - data, - vars = c("x", "xmin", "xmax", "y"), - name = "position_stack" - ) + vars <- intersect(c("x", "xmin", "xmax", "y"), names(data)) + missing <- detect_missing(data, vars) + data[missing, vars] <- NA + flip_data(data, params$flipped_aes) }, diff --git a/tests/testthat/test-geom-col.R b/tests/testthat/test-geom-col.R index 456f03142f..17c61064d9 100644 --- a/tests/testthat/test-geom-col.R +++ b/tests/testthat/test-geom-col.R @@ -7,8 +7,8 @@ test_that("geom_col removes columns with parts outside the plot limits", { ggplotGrob(p + ylim(0.5, 4)), "Removed 3 rows containing missing values or values outside the scale range" ) - expect_warning( # warning created at build stage - ggplot_build(p + ylim(0, 2.5)), + expect_warning( # warning created at render stage + ggplotGrob(p + ylim(0, 2.5)), "Removed 1 row containing missing values or values outside the scale range" ) }) diff --git a/tests/testthat/test-scales.R b/tests/testthat/test-scales.R index 4893caceb4..25c8cc58f9 100644 --- a/tests/testthat/test-scales.R +++ b/tests/testthat/test-scales.R @@ -111,10 +111,14 @@ test_that("oob affects position values", { } base + scale_y_continuous(limits = c(-0,5)) - expect_warning(low_censor <- cdata(base + y_scale(c(0, 5), censor)), + low_censor <- cdata(base + y_scale(c(0, 5), censor)) + mid_censor <- cdata(base + y_scale(c(3, 7), censor)) + handle <- GeomBar$handle_na + + expect_warning(low_censor[[1]] <- handle(low_censor[[1]], list(na.rm = FALSE)), "Removed 1 row containing missing values or values outside the scale range") - expect_warning(mid_censor <- cdata(base + y_scale(c(3, 7), censor)), - "Removed 2 rows containing missing values or values outside the scale range") + expect_warning(mid_censor[[1]] <- handle(mid_censor[[1]], list(na.rm = FALSE)), + "Removed 3 rows containing missing values or values outside the scale range") low_squish <- cdata(base + y_scale(c(0, 5), squish)) mid_squish <- cdata(base + y_scale(c(3, 7), squish)) @@ -127,7 +131,7 @@ test_that("oob affects position values", { # Bars depend on limits and oob expect_equal(low_censor[[1]]$y, c(0.2, 1)) - expect_equal(mid_censor[[1]]$y, c(0.5)) + expect_equal(mid_censor[[1]]$y, numeric(0)) expect_equal(low_squish[[1]]$y, c(0.2, 1, 1)) expect_equal(mid_squish[[1]]$y, c(0, 0.5, 1)) })