tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Version 3.5.1 breaks passing of theme elements for ggplot2 extension #5947

Closed const-ae closed 2 weeks ago

const-ae commented 2 weeks ago

Hi,

a user reported a problem in ggupset which only shows up with ggplot2 version 3.5.1 but not 3.5.0 (https://github.com/const-ae/ggupset/issues/43)!

The problem is that the custom theme elements for ggupset are not properly passed along, which means that they cease to have an effect.

Below is the code which produced the correct output with v3.5.0:

library(tidyverse)
library(ggupset)
tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
    geom_bar() +
    scale_x_upset(n_intersections = 20, order_by = "degree") +
    theme_combmatrix(combmatrix.panel.line.size = 0.1,
                     combmatrix.panel.point.size = 1)
#> Warning: Removed 1176 rows containing non-finite outside the scale range
#> (`stat_count()`).

Created on 2024-06-18 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31) #> os macOS Sonoma 14.5 #> system x86_64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2024-06-18 #> pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.0) #> colorspace 2.1-0 2023-01-23 [2] CRAN (R 4.3.0) #> curl 5.2.1 2024-03-01 [1] CRAN (R 4.3.2) #> digest 0.6.34 2024-01-11 [1] CRAN (R 4.3.0) #> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.0) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.0) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.0) #> farver 2.1.1 2022-07-06 [2] CRAN (R 4.3.0) #> fastmap 1.1.1 2023-02-24 [2] CRAN (R 4.3.0) #> forcats * 1.0.0 2023-01-29 [2] CRAN (R 4.3.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.0) #> generics 0.1.3 2022-07-05 [2] CRAN (R 4.3.0) #> ggplot2 * 3.5.0 2024-06-18 [1] Github (tidyverse/ggplot2@a6f1a7c) #> ggupset * 0.3.0.9002 2023-08-16 [1] local #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.0) #> gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.0) #> highr 0.10 2022-12-22 [2] CRAN (R 4.3.0) #> hms 1.1.3 2023-03-21 [2] CRAN (R 4.3.0) #> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.0) #> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.0) #> labeling 0.4.3 2023-08-29 [1] CRAN (R 4.3.0) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.0) #> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [2] CRAN (R 4.3.0) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 4.3.0) #> pillar 1.9.0 2023-03-22 [2] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.3.0) #> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) #> R.cache 0.16.0 2022-07-21 [2] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [2] CRAN (R 4.3.0) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> readr * 2.1.5 2024-01-10 [1] CRAN (R 4.3.0) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.3.0) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.0) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.0) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.0) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.3.0) #> stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.0) #> stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.0) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.0) #> tibble * 3.2.1 2023-03-20 [2] CRAN (R 4.3.0) #> tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.3.2) #> tidyselect 1.2.0 2022-10-10 [2] CRAN (R 4.3.0) #> tidyverse * 2.0.0 2023-02-22 [2] CRAN (R 4.3.0) #> timechange 0.3.0 2024-01-18 [1] CRAN (R 4.3.0) #> tzdb 0.4.0 2023-05-12 [2] CRAN (R 4.3.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.0) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.0) #> xfun 0.42 2024-02-08 [1] CRAN (R 4.3.2) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.3.0) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.0) #> #> [1] /Users/ahlmanne/Library/R/x86_64/4.3/library #> [2] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

And here is the same code run with version 3.5.1:

library(tidyverse)
library(ggupset)
tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
    geom_bar() +
    scale_x_upset(n_intersections = 20, order_by = "degree") +
    theme_combmatrix(combmatrix.panel.line.size = 0.1,
                     combmatrix.panel.point.size = 1)
#> Warning: Removed 1176 rows containing non-finite outside the scale range
#> (`stat_count()`).

Created on 2024-06-18 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31) #> os macOS Sonoma 14.5 #> system x86_64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2024-06-18 #> pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.0) #> colorspace 2.1-0 2023-01-23 [2] CRAN (R 4.3.0) #> curl 5.2.1 2024-03-01 [1] CRAN (R 4.3.2) #> digest 0.6.34 2024-01-11 [1] CRAN (R 4.3.0) #> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.0) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.0) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.0) #> farver 2.1.1 2022-07-06 [2] CRAN (R 4.3.0) #> fastmap 1.1.1 2023-02-24 [2] CRAN (R 4.3.0) #> forcats * 1.0.0 2023-01-29 [2] CRAN (R 4.3.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.0) #> generics 0.1.3 2022-07-05 [2] CRAN (R 4.3.0) #> ggplot2 * 3.5.1 2024-04-23 [1] CRAN (R 4.3.2) #> ggupset * 0.3.0.9002 2023-08-16 [1] local #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.0) #> gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.0) #> highr 0.10 2022-12-22 [2] CRAN (R 4.3.0) #> hms 1.1.3 2023-03-21 [2] CRAN (R 4.3.0) #> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.0) #> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.0) #> labeling 0.4.3 2023-08-29 [1] CRAN (R 4.3.0) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.0) #> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [2] CRAN (R 4.3.0) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 4.3.0) #> pillar 1.9.0 2023-03-22 [2] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.3.0) #> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) #> R.cache 0.16.0 2022-07-21 [2] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [2] CRAN (R 4.3.0) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> readr * 2.1.5 2024-01-10 [1] CRAN (R 4.3.0) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.3.0) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.0) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.0) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.0) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.3.0) #> stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.0) #> stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.0) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.0) #> tibble * 3.2.1 2023-03-20 [2] CRAN (R 4.3.0) #> tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.3.2) #> tidyselect 1.2.0 2022-10-10 [2] CRAN (R 4.3.0) #> tidyverse * 2.0.0 2023-02-22 [2] CRAN (R 4.3.0) #> timechange 0.3.0 2024-01-18 [1] CRAN (R 4.3.0) #> tzdb 0.4.0 2023-05-12 [2] CRAN (R 4.3.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.0) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.0) #> xfun 0.42 2024-02-08 [1] CRAN (R 4.3.2) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.3.0) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.0) #> #> [1] /Users/ahlmanne/Library/R/x86_64/4.3/library #> [2] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Is this change intentional? I looked through the changelog, but couldn't find any indication if this is something I would need to fix on the ggupset side.

teunbrand commented 2 weeks ago

I'm pretty sure this must be the result of #5743. That PR removes unknown theme elements with a warning, whereas they threw errors before.

I'm not sure why theme_combmatrix() didn't cause errors before, but it appears unknown elements are removed now whereas they weren't before. Unknown elements should be removed, and if they shouldn't, they should be made known by registering them.

library(tidyverse)
library(ggupset)

p <- tidy_movies %>%
  distinct(title, year, length, .keep_all=TRUE) %>%
  ggplot(aes(x=Genres)) +
  geom_bar() +
  scale_x_upset(n_intersections = 20, order_by = "degree") +
  theme_combmatrix(combmatrix.panel.line.size = 0.1,
                   combmatrix.panel.point.size = 1)

p
#> Warning: Removed 1176 rows containing non-finite outside the scale range
#> (`stat_count()`).

Now with element registration:

register_theme_elements(
  combmatrix.panel.line.size = 1.2,
  combmatrix.panel.point.size = 3,
  element_tree = list(
    combmatrix.panel.line.size = el_def("numeric"),
    combmatrix.panel.point.size = el_def("numeric")
  )
)

p
#> Warning: Removed 1176 rows containing non-finite outside the scale range
#> (`stat_count()`).

Created on 2024-06-18 with reprex v2.1.0

const-ae commented 2 weeks ago

Thanks for the quickly reply. I did set validate = FALSE when passing the custom theme elements along (https://github.com/const-ae/ggupset/blob/master/R/theme_combmatrix.R), maybe that's why it didn't trigger an error before?

It's been a while that I worked on ggupset, is it correct that I will need to put

register_theme_elements(
  combmatrix.panel.line.size = 1.2,
  combmatrix.panel.point.size = 3,
# all other theme elements...
  element_tree = list(
    combmatrix.panel.line.size = el_def("numeric"),
    combmatrix.panel.point.size = el_def("numeric")
  )
)

into the .onLoad function of ggupset?

teunbrand commented 2 weeks ago

Yes you're right that the code has to run in the .onLoad hook. You could check if it works by using e.g. + theme(combmatrix.panel.line.color = "red") as the regular theme() function should now also accept your custom elements.

const-ae commented 2 weeks ago

Great. I will try and fix this in the next days :)