Skip to content

Commit

Permalink
Merge branch 'empty_coord_radial_axes' of https://github.com/teunbran…
Browse files Browse the repository at this point in the history
…d/ggplot2 into empty_coord_radial_axes
  • Loading branch information
teunbrand committed Jan 8, 2025
2 parents f6a1607 + 7946aa9 commit bad0de2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* `coord_radial()` now displays no axis instead of throwing an error when
a scale has no breaks (@teunbrand, #6271).
* `geom_ribbon()` now appropriately warns about, and removes, missing values
(@teunbrand, #6243).
* `guide_*()` can now accept two inside legend theme elements:
`legend.position.inside` and `legend.justification.inside`, allowing inside
legends to be placed at different positions. Only inside legends with the same
Expand Down
27 changes: 25 additions & 2 deletions R/geom-ribbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,31 @@ GeomRibbon <- ggproto("GeomRibbon", Geom,

draw_key = draw_key_polygon,

handle_na = function(data, params) {
handle_na = function(self, data, params) {

vars <- vapply(
strsplit(self$required_aes, "|", fixed = TRUE),
`[[`, i = 1, character(1)
)
if (params$flipped_aes || any(data$flipped_aes) %||% FALSE) {
vars <- switch_orientation(vars)
}
vars <- c(vars, self$non_missing_aes)

missing <- detect_missing(data, vars, finite = FALSE)
if (!any(missing)) {
return(data)
}
# We're rearranging groups to account for missing values
data$group <- vec_identify_runs(data_frame0(missing, data$group))
data <- vec_slice(data, !missing)

if (!params$na.rm) {
cli::cli_warn(
"Removed {sum(missing)} row{?s} containing missing values or values \\
outside the scale range ({.fn {snake_class(self)}})."
)
}
data
},

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

# Check that aesthetics are constant
Expand Down
4 changes: 3 additions & 1 deletion R/limits.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#' scales. By default, any values outside the limits specified are replaced with
#' `NA`. Be warned that this will remove data outside the limits and this can
#' produce unintended results. For changing x or y axis limits \strong{without}
#' dropping data observations, see [coord_cartesian()].
#' dropping data observations, see
#' [`coord_cartesian(xlim, ylim)`][coord_cartesian], or use a full scale with
#' [`oob = scales::oob_keep`][scales::oob_keep].
#'
#' @param ... For `xlim()` and `ylim()`: Two numeric values, specifying the left/lower
#' limit and the right/upper limit of the scale. If the larger value is given first,
Expand Down
4 changes: 3 additions & 1 deletion man/lims.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/geom-ribbon.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

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

# NAs are dropped from the data

Removed 1 row containing missing values or values outside the scale range (`geom_ribbon()`).

8 changes: 7 additions & 1 deletion tests/testthat/test-geom-ribbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ test_that("geom_ribbon() checks the aesthetics", {
expect_snapshot_error(geom_ribbon(aes(year, ymin = level - 5, ymax = level + 5), outline.type = "test"))
})

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

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

expect_equal(get_layer_data(p)$ymin, c(0, 0, NA, 0, 0))
expect_snapshot_warning(
grob <- get_layer_grob(p)[[1]]
)
# We expect the ribbon to be broken up into 2 parts
expect_length(grob$children, 2)
})

test_that("geom_ribbon works in both directions", {
Expand Down

0 comments on commit bad0de2

Please sign in to comment.