thomasp85/patchwork

strange figure widths in complex layout

aghaynes opened this issue ยท 4 comments

I have been asked to layout some figures in a slightly novel fashion - one large figure at the top and two rows of smaller figures, similar to the figure below.

The problem is that the second column has a different width than the others. Adding the widths argument to plot_layout has no effect. Is there a way to get the desired layout where the 10 lower panels all have the same width? I'm not sure whether this is a bug or a feature... ๐Ÿ˜•

grafik

The code to create the figure follows

design <- "###AAAA###
           ###AAAA###
           BBCCDDEEFF
           JJKKLLMMNN"
f <- ggplot(mtcars, aes(x = cyl, y = mpg))
f + f + f + f + f + f + f + f + f + f + f + f +
  plot_layout(design = design)

I am running into what I think is the same issue. Edit: This appears to be the same as #289

library(ggplot2)
library(patchwork)

data(mtcars)

p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
        geom_smooth()

p2 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_smooth(color = "red")

p3 <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_smooth(color = "purple")

layout <- "
#AA#
BBCC
"

p.all = p1 + p2 + p3 + 
  plot_layout(design = layout) + plot_annotation(tag_levels = "A")

p.all
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Created on 2023-04-05 with reprex v2.0.2

โ”€ Session info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 setting  value
 version  R version 4.2.2 (2022-10-31)
 os       macOS Ventura 13.0
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2023-04-05
 rstudio  2022.12.0+353 Elsbeth Geranium (desktop)
 pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)

โ”€ Packages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 package       * version date (UTC) lib source
 assertthat      0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
 backports       1.4.1   2021-12-13 [1] CRAN (R 4.2.0)
 broom           1.0.3   2023-01-25 [1] CRAN (R 4.2.0)
 callr           3.7.3   2022-11-02 [1] CRAN (R 4.2.0)
 cellranger      1.1.0   2016-07-27 [1] CRAN (R 4.2.0)
 cli             3.6.0   2023-01-09 [1] CRAN (R 4.2.0)
 clipr           0.8.0   2022-02-22 [1] CRAN (R 4.2.0)
 colorspace      2.1-0   2023-01-23 [1] CRAN (R 4.2.0)
 crayon          1.5.2   2022-09-29 [1] CRAN (R 4.2.0)
 DBI             1.1.3   2022-06-18 [1] CRAN (R 4.2.0)
 dbplyr          2.2.1   2022-06-27 [1] CRAN (R 4.2.2)
 digest          0.6.31  2022-12-11 [1] CRAN (R 4.2.0)
 dplyr         * 1.1.0   2023-01-29 [1] CRAN (R 4.2.0)
 ellipsis        0.3.2   2021-04-29 [1] CRAN (R 4.2.0)
 evaluate        0.20    2023-01-17 [1] CRAN (R 4.2.0)
 fansi           1.0.4   2023-01-22 [1] CRAN (R 4.2.0)
 farver          2.1.1   2022-07-06 [1] CRAN (R 4.2.0)
 fastmap         1.1.1   2023-02-24 [1] CRAN (R 4.2.0)
 forcats       * 1.0.0   2023-01-29 [1] CRAN (R 4.2.0)
 fs              1.6.1   2023-02-06 [1] CRAN (R 4.2.0)
 gargle          1.3.0   2023-01-30 [1] CRAN (R 4.2.0)
 generics        0.1.3   2022-07-05 [1] CRAN (R 4.2.0)
 ggplot2       * 3.4.1   2023-02-10 [1] CRAN (R 4.2.0)
 glue            1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
 googledrive     2.0.0   2021-07-08 [1] CRAN (R 4.2.0)
 googlesheets4   1.0.1   2022-08-13 [1] CRAN (R 4.2.0)
 gtable          0.3.1   2022-09-01 [1] CRAN (R 4.2.0)
 haven           2.5.1   2022-08-22 [1] CRAN (R 4.2.0)
 hms             1.1.2   2022-08-19 [1] CRAN (R 4.2.0)
 htmltools       0.5.4   2022-12-07 [1] CRAN (R 4.2.0)
 httr            1.4.4   2022-08-17 [1] CRAN (R 4.2.0)
 jsonlite        1.8.4   2022-12-06 [1] CRAN (R 4.2.0)
 knitr           1.42    2023-01-25 [1] CRAN (R 4.2.0)
 labeling        0.4.2   2020-10-20 [1] CRAN (R 4.2.0)
 lattice         0.20-45 2021-09-22 [1] CRAN (R 4.2.2)
 lifecycle       1.0.3   2022-10-07 [1] CRAN (R 4.2.0)
 lubridate       1.9.2   2023-02-10 [1] CRAN (R 4.2.0)
 magrittr        2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
 Matrix          1.5-1   2022-09-13 [1] CRAN (R 4.2.2)
 mgcv            1.8-41  2022-10-21 [1] CRAN (R 4.2.2)
 modelr          0.1.10  2022-11-11 [1] CRAN (R 4.2.0)
 munsell         0.5.0   2018-06-12 [1] CRAN (R 4.2.0)
 nlme            3.1-160 2022-10-10 [1] CRAN (R 4.2.2)
 patchwork     * 1.1.2   2022-08-19 [1] CRAN (R 4.2.0)
 pillar          1.8.1   2022-08-19 [1] CRAN (R 4.2.0)
 pkgconfig       2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
 processx        3.8.0   2022-10-26 [1] CRAN (R 4.2.0)
 ps              1.7.2   2022-10-26 [1] CRAN (R 4.2.0)
 purrr         * 1.0.1   2023-01-10 [1] CRAN (R 4.2.0)
 R.cache         0.16.0  2022-07-21 [1] CRAN (R 4.2.0)
 R.methodsS3     1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
 R.oo            1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
 R.utils         2.12.2  2022-11-11 [1] CRAN (R 4.2.0)
 R6              2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
 readr         * 2.1.3   2022-10-01 [1] CRAN (R 4.2.0)
 readxl          1.4.1   2022-08-17 [1] CRAN (R 4.2.0)
 reprex          2.0.2   2022-08-17 [1] CRAN (R 4.2.0)
 rlang           1.0.6   2022-09-24 [1] CRAN (R 4.2.0)
 rmarkdown       2.20    2023-01-19 [1] CRAN (R 4.2.0)
 rstudioapi      0.14    2022-08-22 [1] CRAN (R 4.2.0)
 rvest           1.0.3   2022-08-19 [1] CRAN (R 4.2.0)
 scales          1.2.1   2022-08-20 [1] CRAN (R 4.2.0)
 sessioninfo     1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
 stringi         1.7.12  2023-01-11 [1] CRAN (R 4.2.0)
 stringr       * 1.5.0   2022-12-02 [1] CRAN (R 4.2.0)
 styler          1.9.0   2023-01-15 [1] CRAN (R 4.2.0)
 tibble        * 3.2.0   2023-03-08 [1] CRAN (R 4.2.0)
 tidyr         * 1.3.0   2023-01-24 [1] CRAN (R 4.2.0)
 tidyselect      1.2.0   2022-10-10 [1] CRAN (R 4.2.0)
 tidyverse     * 1.3.2   2022-07-18 [1] CRAN (R 4.2.0)
 timechange      0.2.0   2023-01-11 [1] CRAN (R 4.2.0)
 tzdb            0.3.0   2022-03-28 [1] CRAN (R 4.2.0)
 utf8            1.2.3   2023-01-31 [1] CRAN (R 4.2.0)
 vctrs           0.5.2   2023-01-23 [1] CRAN (R 4.2.0)
 withr           2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
 xfun            0.37    2023-01-31 [1] CRAN (R 4.2.0)
 xml2            1.3.3   2021-11-30 [1] CRAN (R 4.2.0)
 yaml            2.3.7   2023-01-23 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

Also hitting this, with:

library(patchwork)
layout <- "
AABBCCC
DDDEEFF
"

test <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()

test + test +test+test+test+test + plot_layout(design = layout)

the issue is that the y-axis of the top plot influences the width of plots spanning it's location. The easiest way is to remove alignment from the top plot by putting it inside wrap_elements()

library(ggplot2)
library(patchwork)

design <- "###AAAA###
           ###AAAA###
           BBCCDDEEFF
           JJKKLLMMNN"
f <- ggplot(mtcars, aes(x = cyl, y = mpg))
wrap_elements(full =  f) + f + f + f + f + f + f + f + f + f + f +
  plot_layout(design = design)

Created on 2023-08-07 by the reprex package (v2.0.1)

so easy... thanks @thomasp85!