Skip to content

Commit aa41dcb

Browse files
authored
position_stack() does not remove missing data (#5519)
* instead of removing, `position_stack()` sets incomplete data to missing * adjust test * Add news bullet * add issue nr to bullet
1 parent e5abb05 commit aa41dcb

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `position_stack()` no longer silently removes missing data, which is now
4+
handled by the geom instead of position (#3532).
5+
36
* Legend keys that can draw arrows have their size adjusted for arrows.
47

58
* The `trans` argument in scales and secondary axes has been renamed to

R/position-stack.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ PositionStack <- ggproto("PositionStack", Position,
175175
ymax = as.numeric(ifelse(data$ymax == 0, data$ymin, data$ymax))
176176
)
177177

178-
data <- remove_missing(
179-
data,
180-
vars = c("x", "xmin", "xmax", "y"),
181-
name = "position_stack"
182-
)
178+
vars <- intersect(c("x", "xmin", "xmax", "y"), names(data))
179+
missing <- detect_missing(data, vars)
180+
data[missing, vars] <- NA
181+
183182
flip_data(data, params$flipped_aes)
184183
},
185184

tests/testthat/test-geom-col.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ test_that("geom_col removes columns with parts outside the plot limits", {
77
ggplotGrob(p + ylim(0.5, 4)),
88
"Removed 3 rows containing missing values or values outside the scale range"
99
)
10-
expect_warning( # warning created at build stage
11-
ggplot_build(p + ylim(0, 2.5)),
10+
expect_warning( # warning created at render stage
11+
ggplotGrob(p + ylim(0, 2.5)),
1212
"Removed 1 row containing missing values or values outside the scale range"
1313
)
1414
})

tests/testthat/test-scales.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ test_that("oob affects position values", {
111111
}
112112
base + scale_y_continuous(limits = c(-0,5))
113113

114-
expect_warning(low_censor <- cdata(base + y_scale(c(0, 5), censor)),
114+
low_censor <- cdata(base + y_scale(c(0, 5), censor))
115+
mid_censor <- cdata(base + y_scale(c(3, 7), censor))
116+
handle <- GeomBar$handle_na
117+
118+
expect_warning(low_censor[[1]] <- handle(low_censor[[1]], list(na.rm = FALSE)),
115119
"Removed 1 row containing missing values or values outside the scale range")
116-
expect_warning(mid_censor <- cdata(base + y_scale(c(3, 7), censor)),
117-
"Removed 2 rows containing missing values or values outside the scale range")
120+
expect_warning(mid_censor[[1]] <- handle(mid_censor[[1]], list(na.rm = FALSE)),
121+
"Removed 3 rows containing missing values or values outside the scale range")
118122

119123
low_squish <- cdata(base + y_scale(c(0, 5), squish))
120124
mid_squish <- cdata(base + y_scale(c(3, 7), squish))
@@ -127,7 +131,7 @@ test_that("oob affects position values", {
127131

128132
# Bars depend on limits and oob
129133
expect_equal(low_censor[[1]]$y, c(0.2, 1))
130-
expect_equal(mid_censor[[1]]$y, c(0.5))
134+
expect_equal(mid_censor[[1]]$y, numeric(0))
131135
expect_equal(low_squish[[1]]$y, c(0.2, 1, 1))
132136
expect_equal(mid_squish[[1]]$y, c(0, 0.5, 1))
133137
})

0 commit comments

Comments
 (0)