thomasp85 / patchwork

The Composer of ggplots
https://patchwork.data-imaginist.com
Other
2.48k stars 163 forks source link

guides = 'collect' does not work with latest github version of ggplot2 #374

Open BenVolpe94 opened 3 months ago

BenVolpe94 commented 3 months ago

Hi! I'm currently using the latest github version of ggplot2 because it has a fix for an issue I encountered. I noticed however that this version broke the guides = 'collect' option in patchwork.

See example below:

# Load current main branch
devtools::dev_mode(on = T)
#> ✔ Dev mode: ON
library(ggplot2)
library(patchwork)

# Create plots
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

# Combine plots
p1 + p2 + p3 + p4 +
  plot_layout(guides = 'collect')
#> Error in identicalUnits(x): object is not a unit

# Session info
sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 11 x64 (build 22635)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Dutch_Belgium.utf8  LC_CTYPE=Dutch_Belgium.utf8   
#> [3] LC_MONETARY=Dutch_Belgium.utf8 LC_NUMERIC=C                  
#> [5] LC_TIME=Dutch_Belgium.utf8    
#> 
#> time zone: Europe/Brussels
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] patchwork_1.2.0    ggplot2_3.5.1.9000
#> 
#> loaded via a namespace (and not attached):
#>  [1] utf8_1.2.4        generics_0.1.3    stringi_1.8.4     digest_0.6.36    
#>  [5] magrittr_2.0.3    evaluate_0.24.0   grid_4.3.3        pkgload_1.4.0    
#>  [9] fastmap_1.2.0     pkgbuild_1.4.4    sessioninfo_1.2.2 urlchecker_1.0.1 
#> [13] promises_1.3.0    purrr_1.0.2       fansi_1.0.6       scales_1.3.0     
#> [17] cli_3.6.3.9000    shiny_1.9.1       rlang_1.1.4       ellipsis_0.3.2   
#> [21] munsell_0.5.1     reprex_2.1.1      remotes_2.5.0     withr_3.0.1      
#> [25] cachem_1.1.0      yaml_2.3.10       devtools_2.4.5    tools_4.3.3      
#> [29] memoise_2.0.1     dplyr_1.1.4       colorspace_2.1-1  httpuv_1.6.15    
#> [33] vctrs_0.6.5       R6_2.5.1          mime_0.12         lifecycle_1.0.4  
#> [37] stringr_1.5.1     fs_1.6.4          htmlwidgets_1.6.4 usethis_3.0.0    
#> [41] miniUI_0.1.1.1    pkgconfig_2.0.3   pillar_1.9.0      later_1.3.2      
#> [45] gtable_0.3.5      glue_1.7.0        profvis_0.3.8     Rcpp_1.0.13      
#> [49] xfun_0.46         tibble_3.2.1      tidyselect_1.2.1  rstudioapi_0.16.0
#> [53] knitr_1.48        farver_2.1.2      xtable_1.8-4      htmltools_0.5.8.1
#> [57] labeling_0.4.3    rmarkdown_2.27    compiler_4.3.3

I get the following Traceback:

Error in identicalUnits(x) : object is not a unit
7. identicalUnits(x)
6. unit.c(legend.box.margin[4], widths, legend.box.margin[2])
5. guides_build(guides, theme)
4. assemble_guides(guide_grobs, theme)
3. build_patchwork(plot, plot$layout$guides %||% "auto")
2. print.patchwork(x)
1. (function (x, ...)
UseMethod("print"))(x)
thomasp85 commented 3 months ago

This seems to be fixed with the dev version of patchwork - have you tried with that?

trekonom commented 2 months ago

Can confirm that the code works fine using the dev version of patchwork:

library(tidyverse)
library(patchwork)

packageVersion("ggplot2")
#> [1] '3.5.1.9000'
packageVersion("patchwork")
#> [1] '1.2.0.9000'

library(ggplot2)
library(patchwork)

# Create plots
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

# Combine plots
p1 + p2 + p3 + p4 +
  plot_layout(guides = 'collect')

Created on 2024-09-08 with reprex v2.1.1

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.1 (2024-06-14) #> os macOS Sonoma 14.6.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2024-09-08 #> pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.0) #> colorspace 2.1-1 2024-07-26 [1] CRAN (R 4.4.0) #> curl 5.2.2 2024-08-26 [1] CRAN (R 4.4.1) #> digest 0.6.37 2024-08-19 [1] CRAN (R 4.4.1) #> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.4.0) #> evaluate 0.24.0 2024-06-10 [1] CRAN (R 4.4.0) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.4.0) #> farver 2.1.2 2024-05-13 [1] CRAN (R 4.4.0) #> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.0) #> forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.4.0) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.4.0) #> ggplot2 * 3.5.1.9000 2024-09-08 [1] Github (tidyverse/ggplot2@eec7490) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.0) #> gtable 0.3.5 2024-04-22 [1] CRAN (R 4.4.0) #> highr 0.11 2024-05-26 [1] CRAN (R 4.4.0) #> hms 1.1.3 2023-03-21 [1] CRAN (R 4.4.0) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0) #> knitr 1.48 2024-07-07 [1] CRAN (R 4.4.0) #> labeling 0.4.3 2023-08-29 [1] CRAN (R 4.4.0) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0) #> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0) #> munsell 0.5.1 2024-04-01 [1] CRAN (R 4.4.0) #> patchwork * 1.2.0.9000 2024-09-08 [1] Github (thomasp85/patchwork@02dc38b) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.4.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.4.0) #> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.4.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0) #> readr * 2.1.5 2024-01-10 [1] CRAN (R 4.4.0) #> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.4.0) #> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.0) #> rmarkdown 2.28 2024-08-17 [1] CRAN (R 4.4.0) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.4.0) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0) #> stringi 1.8.4 2024-05-06 [1] CRAN (R 4.4.0) #> stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.4.0) #> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.4.0) #> tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.4.0) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.4.0) #> tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.4.0) #> timechange 0.3.0 2024-01-18 [1] CRAN (R 4.4.0) #> tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.4.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.4.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0) #> withr 3.0.1 2024-07-31 [1] CRAN (R 4.4.0) #> xfun 0.47 2024-08-17 [1] CRAN (R 4.4.0) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.4.0) #> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.4.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```