Skip to content

Quirky NA-handling of geom_ribbon() #6243

Closed
@teunbrand

Description

@teunbrand

Now that we're allowing gradients in geom_ribbon() and derivatives, the following caught my attention.

In the example below, we simply want to vary the gradient along x, so top and bottom gradients should match.
However, the gradient is misplaced because the second group has a missing value (note it starts with dark blue in the middle).

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

df <- data.frame(
  x = c(1:3, 1:3),
  ymin = c(1, 1, 1, NA, 2, 2),
  ymax = c(1.5, 2, 1.5, 2.5, 3, 2.5),
  group = c(1,1,1,2,2,2)
)

p <- ggplot(df, aes(x, ymin = ymin, ymax = ymax, group = group, fill = x)) +
  scale_fill_viridis_c()

# Wrong
p + geom_ribbon()

The gradients are drawn correctly if missing values are removed properly.

p + geom_ribbon(na.rm = TRUE)

Created on 2024-12-16 with reprex v2.1.1

I think there are two things wrong with this when na.rm = FALSE:

  • There is no warning about missing values, as you would expect with any other geom
  • It doesn't actually remove the missing data, causing the misalignment of gradients.

I traced this decision to a remedy against #1549.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions