Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New warning missing values or values outside the scale range with geom_area(position = position_fill()) #6338

Open
jbengler opened this issue Feb 19, 2025 · 4 comments · May be fixed by #6341
Labels
bug an unexpected problem or unintended behavior positions 🥇

Comments

@jbengler
Copy link

Dear @teunbrand

I am struggling to wrap my head around this new warning message that occurs with ggplot2 v3.5.1.9000 but not v3.5.1.

This might be connected to #6211 and #6269, which report the same warning message but in different contexts.

Best
Jan

mtcars |>
  ggplot(aes(x = am, y = gear, fill = cyl, group = cyl)) +
  geom_area()

# works

mtcars |>
  ggplot(aes(x = am, y = gear, fill = cyl, group = cyl)) +
  geom_area(position = position_fill())

# Warning message:
# Removed 6 rows containing missing values or values outside the scale range (`geom_area()`).
@teunbrand
Copy link
Collaborator

Hi Jan!

I don't think the similarity of the error message is sufficient to implicate #6211 or #6269. I'm reasonably sure that the change you're seeing is because of #6244. The data does contain missing values (note the first y), so these should be warned about but that didn't happen earlier.

library(ggplot2)

p <- mtcars |>
  ggplot(aes(x = am, y = gear, fill = cyl, group = cyl)) +
  geom_area(position = position_fill())

layer_data(p) |> head()
#>      fill      x  y group PANEL align_padding flipped_aes      ymin ymax   xmin
#> 1 #132B43 -0.002 NA     1     1          TRUE       FALSE        NA   NA -0.002
#> 2 #132B43 -0.001  1     1     1         FALSE       FALSE 0.6000000    1 -0.001
#> 3 #132B43  0.000  1     1     1         FALSE       FALSE 0.7000000    1  0.000
#> 4 #132B43  0.001  1     1     1         FALSE       FALSE 0.6999900    1  0.001
#> 5 #132B43  0.999  1     1     1         FALSE       FALSE 0.6923077    1  0.999
#> 6 #132B43  1.000  1     1     1         FALSE       FALSE 0.7142857    1  1.000
#>     xmax colour linewidth linetype alpha
#> 1 -0.002     NA       0.5        1    NA
#> 2 -0.001     NA       0.5        1    NA
#> 3  0.000     NA       0.5        1    NA
#> 4  0.001     NA       0.5        1    NA
#> 5  0.999     NA       0.5        1    NA
#> 6  1.000     NA       0.5        1    NA

Created on 2025-02-19 with reprex v2.1.1

@jbengler
Copy link
Author

Hey Teun!

Thank you very much for your prompt reply!

The option na.rm = TRUE solves my problem.

However, I am still not sure where the missing values are actually coming from. Consider the following simplified example.

df <-
  tibble::tribble(
    ~season,   ~family, ~count,
    "Summer",    "Bird",    10,
    "Summer",    "Fish",    12,
    "Winter",    "Bird",     4,
    "Winter",    "Fish",     1
  )

df |>
  ggplot(aes(x = season, y = count, fill = family, group = family)) +
  geom_area()

df |>
  ggplot(aes(x = season, y = count, fill = family, group = family)) +
  geom_area(position = position_fill())

@teunbrand
Copy link
Collaborator

teunbrand commented Feb 19, 2025

So stat = "align" sets the first observation to x = 0.999, y = 0 for both groups. Then position_fill() 'stacks' these zeroes in the lines below:

ggplot2/R/position-stack.R

Lines 224 to 228 in d835cfe

heights <- c(0, cumsum(y))
if (fill) {
heights <- heights / abs(heights[length(heights)])
}

From the code above we can anticipate a 'divide by zero' error, and 0 / 0 == NaN, so that is where the missing values are coming from. Setting stat = "identity" would fix the problem.

However, it might make sense to avoid the divide by zero error too. I'll take that to be the action prompted by this issue then.

@teunbrand teunbrand added bug an unexpected problem or unintended behavior positions 🥇 labels Feb 19, 2025
@jbengler
Copy link
Author

Sounds great. Thanks again!

@teunbrand teunbrand linked a pull request Feb 19, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior positions 🥇
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants