Skip to content

Incorrect warning from geom about missing values when scale sets limits, unless data is filtered first #5250

Closed

Description

@ellisp

I found a problem with a misleading warning from geom_xxx when a scale is truncated with the limits argument

I expected no warnings.

I suspect this is related to or the same problem as #5118 but because the discussion there is histogram-specific I have opened a new issue.

Here is the code to reproduce the bug:

library(tidyverse)

d <- tibble (yr = rep(2000:2020, 2),
             animal = rep(c("cat", "dog"), each = 21),
             value = rnorm(42))

d |>
  ggplot(aes(x = yr, y = value, colour = animal)) +
  geom_line() +
  scale_x_continuous(limits = c(2011, 2020))
#> Warning: Removed 22 rows containing missing values (`geom_line()`).

# also happens with other geoms:
d |>
  ggplot(aes(x = yr, y = value, colour = animal)) +
  geom_point() +
  scale_x_continuous(limits = c(2011, 2020))
#> Warning: Removed 22 rows containing missing values (`geom_point()`).

# Warning goes away if data is explicitly filtered to match the scale limits:
d |>
  filter(yr >= 2011) |>
  ggplot(aes(x = yr, y = value, colour = animal)) +
  geom_line() +
  scale_x_continuous(limits = c(2011, 2020))

Created on 2023-03-27 with reprex v2.0.2

Activity

  1. clauswilke commented on Mar 27, 2023

    @clauswilke
    Member

    This has been the intended behavior for as long as I can remember (over a decade?). ggplot warns you when the scale range is smaller than the data range. The warning goes away if you set na.rm = TRUE in the geom:

    library(tidyverse)
    
    d <- tibble (yr = rep(2000:2020, 2),
                 animal = rep(c("cat", "dog"), each = 21),
                 value = rnorm(42))
    
    d |>
      ggplot(aes(x = yr, y = value, colour = animal)) +
      geom_line(na.rm = TRUE) +
      scale_x_continuous(limits = c(2011, 2020))

    Created on 2023-03-27 with reprex v2.0.2

    I agree that the wording of the warning is a bit confusing, but the warning itself is useful. Not sure if it would be possible to catch the specific situation (data values outside the scale limits) and give a different warning in that case, versus other reasons why there may be missing values.

  2. teunbrand commented on Mar 27, 2023

    @teunbrand
    Collaborator

    I agree that the warning is useful and appropriate, but perhaps the wording could be improved.

    The censoring itself occurs in the scales, which doesn't know about in what layer it is censoring data. The dropping of the data occurs in the layers, which don't know whether they see NAs because they were missing from the beginning, or censored by the out-of-bounds handler. This makes it a bit hard to signal a tailored error message.

    Instead, we might just update the wording so that it reflects the possibility of censored data:

    #> Warning: Removed 22 rows containing excluded or missing values (`geom_point()`).
    
  3. added
    messages
    on Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    messages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions