patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Wrapping plots remove individual annotations

Open DanChaltiel opened this issue 4 years ago • 4 comments

Hi Thomas,

If you use plot_annotation to annotate some plots and then wrap them into a patchwork, the annotations are dropped.

Here is an example:

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + 
  plot_annotation(caption = 'I am plot p1')
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear)) + 
  plot_annotation(title = "PLOT2", caption = 'I am plot p2')
wrap_plots(p1, p2)

Is this a bug in wrap_plots or am I missing something? Wouldn't a keep_annotations parameter be useful/doable?

Dan

DanChaltiel avatar Jan 07 '20 07:01 DanChaltiel

Plot annotations are currently reserved for the top level plot, but only for technical reasons. I'll try to fix it in a future release

thomasp85 avatar Jan 07 '20 07:01 thomasp85

It would be great if this could be implemented so that it would be possible to have titles for subsets of plots

achetverikov avatar Jun 23 '20 10:06 achetverikov

Same problem. Temporary solution was to add a textGrob, see below. Trivial, but anyone know how to decrease the textGrob area without the may 2s and 3s in design argument?

Thanks for creating Patchwork, it's great software!

library(ggplot2)
library(patchwork)

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_bar(aes(gear)) + facet_wrap(~cyl) + ggtitle('Plot 3')
p4 <- ggplot(mtcars) + geom_bar(aes(carb)) + ggtitle('Plot 4')
p5 <- ggplot(mtcars) + geom_violin(aes(cyl, mpg, group = cyl)) + ggtitle('Plot 5')

# Problem: Annotation of assembled plots that are again assembled with others is dropped
# Temporary solution: recreate panel as small text grob
# Lots of 2s and 3s: only way I found to decrease size of text grob
design_panelA <- "
  122222223333333
"

panelA <- wrap_elements(grid::textGrob("Panel A", x=.3, y=.5, rot=90)) + 
  p1 + p2 + 
  plot_layout(design = design_panelA)

design_panelB <- "
  1222222233333334444444
"
panelB <- wrap_elements(grid::textGrob("Panel B", x=.3, y=.5, rot=90)) + 
  p3 + p4 + p5 +
  plot_layout(design = design_panelB)

design_plot <- "
  11#
  222
" 
panelA / panelB + plot_layout(design = design_plot)

Created on 2021-06-22 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_CA.UTF-8                 
#>  ctype    en_CA.UTF-8                 
#>  tz       America/Toronto             
#>  date     2021-06-22                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
#>  backports     1.2.1   2020-12-09 [1] CRAN (R 4.1.0)
#>  cli           2.5.0   2021-04-26 [1] CRAN (R 4.1.0)
#>  colorspace    2.0-1   2021-05-04 [1] CRAN (R 4.1.0)
#>  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.1.0)
#>  curl          4.3.1   2021-04-30 [1] CRAN (R 4.1.0)
#>  DBI           1.1.1   2021-01-15 [1] CRAN (R 4.1.0)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.1.0)
#>  dplyr         1.0.6   2021-05-05 [1] CRAN (R 4.1.0)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         0.5.0   2021-05-25 [1] CRAN (R 4.1.0)
#>  farver        2.1.0   2021-02-28 [1] CRAN (R 4.1.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.1.0)
#>  generics      0.1.0   2020-10-31 [1] CRAN (R 4.1.0)
#>  ggplot2     * 3.3.3   2020-12-30 [1] CRAN (R 4.1.0)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.1.0)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.1.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.1.0)
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.0)
#>  knitr         1.33    2021-04-24 [1] CRAN (R 4.1.0)
#>  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.1.0)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
#>  mime          0.10    2021-02-13 [1] CRAN (R 4.1.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.0)
#>  patchwork   * 1.1.1   2020-12-17 [1] CRAN (R 4.1.0)
#>  pillar        1.6.1   2021-05-16 [1] CRAN (R 4.1.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  R6            2.5.0   2020-10-28 [1] CRAN (R 4.1.0)
#>  reprex        2.0.0   2021-04-02 [1] CRAN (R 4.1.0)
#>  rlang         0.4.11  2021-04-30 [1] CRAN (R 4.1.0)
#>  rmarkdown     2.8     2021-05-07 [1] CRAN (R 4.1.0)
#>  scales        1.1.1   2020-05-11 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.1.0)
#>  stringi       1.6.2   2021-05-17 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.4.1   2021-03-30 [1] CRAN (R 4.1.0)
#>  tibble        3.1.2   2021-05-16 [1] CRAN (R 4.1.0)
#>  tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.1.0)
#>  utf8          1.2.1   2021-03-12 [1] CRAN (R 4.1.0)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)
#>  withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.0)
#>  xfun          0.23    2021-05-15 [1] CRAN (R 4.1.0)
#>  xml2          1.3.2   2020-04-23 [1] CRAN (R 4.1.0)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

abanihas avatar Jun 22 '21 22:06 abanihas

Any fix for this ?

albertostefanelli avatar Jul 19 '22 12:07 albertostefanelli