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

ggpattern doesn't seem to work with latest DEV version #5608

Closed
davidhodge931 opened this issue Dec 22, 2023 · 2 comments
Closed

ggpattern doesn't seem to work with latest DEV version #5608

davidhodge931 opened this issue Dec 22, 2023 · 2 comments

Comments

@davidhodge931
Copy link

library(palmerpenguins)
library(tidyverse)
library(ggpattern)

penguins |>
  group_by(species, sex) |>
  summarise(across(body_mass_g, \(x) mean(x))) |>
  ggplot(    mapping = aes(y = species,
                           x = body_mass_g,
                           fill = sex, 
                           pattern = sex)) +
  geom_col_pattern()
#> `summarise()` has grouped output by 'species'. You can override using the
#> `.groups` argument.
#> Warning: The `scale_name` argument of `discrete_scale()` is deprecated as of ggplot2
#> 3.5.0.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_col_pattern()`).
#> Error in if (pat == "regular_polygon" && is.numeric(params$pattern_shape)) params$pattern_shape <- "convex6": missing value where TRUE/FALSE needed

Created on 2023-12-22 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

This is a more minimal reprex

devtools::load_all("~/packages/ggplot2")
# or:
# library(ggplot2) # if dev version is installed
#> ℹ Loading ggplot2
library(ggpattern)

df <- data.frame(x = c(1, 2, 3), y = c(1, 2, NA), fill = c("A", "B", NA))

ggplot(df, aes(x, y, fill = fill, pattern = fill)) +
  geom_col_pattern()
#> Warning: The `scale_name` argument of `discrete_scale()` is deprecated as of ggplot2
#> 3.5.0.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_col_pattern()`).
#> Error in if (pat == "regular_polygon" && is.numeric(params$pattern_shape)) params$pattern_shape <- "convex6": missing value where TRUE/FALSE needed

Seems to be solved by setting a na.value in the scale:

last_plot() + scale_pattern_discrete(na.value = "crosshatch")
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_col_pattern()`).

Created on 2024-01-02 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

Seems to be caused by #5519, where missing values are no longer silently removed by position_stack().
You can trigger the same error (in a different location) with CRAN ggplot2 by setting position = "identity", which also doesn't remove NA values.

library(ggplot2)
library(ggpattern)

df <- data.frame(x = c(1, 2, 3), y = c(1, 2, NA), fill = c("A", "B", NA))

ggplot(df, aes(x, y, fill = fill, pattern = fill)) +
  geom_col_pattern(position = 'identity')
#> Warning: Removed 1 rows containing missing values (`geom_col_pattern()`).
#> Error in if (pat == "regular_polygon" && is.numeric(params$pattern_shape)) params$pattern_shape <- "convex6": missing value where TRUE/FALSE needed

Created on 2024-01-02 with reprex v2.0.2

In any case, as we're unlikely to revert #5519, it seems like the most suitable fix is for {ggpattern} to set a na.value for the discrete scale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants