Skip to content

Commit bad0de2

Browse files
committed
Merge branch 'empty_coord_radial_axes' of https://github.com/teunbrand/ggplot2 into empty_coord_radial_axes
2 parents f6a1607 + 7946aa9 commit bad0de2

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

NEWS.md

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

33
* `coord_radial()` now displays no axis instead of throwing an error when
44
a scale has no breaks (@teunbrand, #6271).
5+
* `geom_ribbon()` now appropriately warns about, and removes, missing values
6+
(@teunbrand, #6243).
57
* `guide_*()` can now accept two inside legend theme elements:
68
`legend.position.inside` and `legend.justification.inside`, allowing inside
79
legends to be placed at different positions. Only inside legends with the same

R/geom-ribbon.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,31 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
126126

127127
draw_key = draw_key_polygon,
128128

129-
handle_na = function(data, params) {
129+
handle_na = function(self, data, params) {
130+
131+
vars <- vapply(
132+
strsplit(self$required_aes, "|", fixed = TRUE),
133+
`[[`, i = 1, character(1)
134+
)
135+
if (params$flipped_aes || any(data$flipped_aes) %||% FALSE) {
136+
vars <- switch_orientation(vars)
137+
}
138+
vars <- c(vars, self$non_missing_aes)
139+
140+
missing <- detect_missing(data, vars, finite = FALSE)
141+
if (!any(missing)) {
142+
return(data)
143+
}
144+
# We're rearranging groups to account for missing values
145+
data$group <- vec_identify_runs(data_frame0(missing, data$group))
146+
data <- vec_slice(data, !missing)
147+
148+
if (!params$na.rm) {
149+
cli::cli_warn(
150+
"Removed {sum(missing)} row{?s} containing missing values or values \\
151+
outside the scale range ({.fn {snake_class(self)}})."
152+
)
153+
}
130154
data
131155
},
132156

@@ -135,7 +159,6 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,
135159
flipped_aes = FALSE, outline.type = "both") {
136160
data <- check_linewidth(data, snake_class(self))
137161
data <- flip_data(data, flipped_aes)
138-
if (na.rm) data <- data[stats::complete.cases(data[c("x", "ymin", "ymax")]), ]
139162
data <- data[order(data$group), ]
140163

141164
# Check that aesthetics are constant

R/limits.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#' scales. By default, any values outside the limits specified are replaced with
55
#' `NA`. Be warned that this will remove data outside the limits and this can
66
#' produce unintended results. For changing x or y axis limits \strong{without}
7-
#' dropping data observations, see [coord_cartesian()].
7+
#' dropping data observations, see
8+
#' [`coord_cartesian(xlim, ylim)`][coord_cartesian], or use a full scale with
9+
#' [`oob = scales::oob_keep`][scales::oob_keep].
810
#'
911
#' @param ... For `xlim()` and `ylim()`: Two numeric values, specifying the left/lower
1012
#' limit and the right/upper limit of the scale. If the larger value is given first,

man/lims.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/geom-ribbon.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323

2424
`outline.type` must be one of "both", "upper", "lower", or "full", not "test".
2525

26+
# NAs are dropped from the data
27+
28+
Removed 1 row containing missing values or values outside the scale range (`geom_ribbon()`).
29+

tests/testthat/test-geom-ribbon.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ test_that("geom_ribbon() checks the aesthetics", {
1313
expect_snapshot_error(geom_ribbon(aes(year, ymin = level - 5, ymax = level + 5), outline.type = "test"))
1414
})
1515

16-
test_that("NAs are not dropped from the data", {
16+
test_that("NAs are dropped from the data", {
1717
df <- data_frame(x = 1:5, y = c(1, 1, NA, 1, 1))
1818

1919
p <- ggplot(df, aes(x))+
2020
geom_ribbon(aes(ymin = y - 1, ymax = y + 1))
21+
p <- ggplot_build(p)
2122

2223
expect_equal(get_layer_data(p)$ymin, c(0, 0, NA, 0, 0))
24+
expect_snapshot_warning(
25+
grob <- get_layer_grob(p)[[1]]
26+
)
27+
# We expect the ribbon to be broken up into 2 parts
28+
expect_length(grob$children, 2)
2329
})
2430

2531
test_that("geom_ribbon works in both directions", {

0 commit comments

Comments
 (0)