Issue with axis placement in plots in a patchwork when `theme(strip.placement = "outside")` is used
In the specific case where theme(strip.placement = "outside") is set on one plot, unexpected spacing is observed between the axes and the plot region on other plots in a patchwork.
In the reprex below, a proportion bar chart is drawn with facets and combined in a patchwork. The proprotion plot is then placed in a patchwork with a proportion plot without facets. In the second image, the same group of proportion plots is placed in a patchwork, but theme(strip.placement = "outside") is applied to the faceted proportion plot. The first patchwork renders as expected, and the second patchwork exhibits the issue with axis placement. If either plot in the second example is printed separately, the axis issue is not observed.
# Mock count proportion dataset
sample_df <-
as_tibble(
list(
population = factor(rep(1:20, 3), levels = 1:20),
subpopulation =
factor(
c(rep("A", 20), rep("B", 20), rep("C", 20)),
levels = c("A", "B", "C")
),
counts = sample(1:150, 60, replace = TRUE)
)
)
sample_df$group <-
case_when(
sample_df$population %in% as.character(c(1:10)) ~ "Group 1",
sample_df$population %in% as.character(c(11:20)) ~ "Group 2"
)
sample_df$group <-
factor(
sample_df$group, levels = c("Group 1", "Group 2")
)
# Proportion bar plot
plot_a <-
ggplot(sample_df, aes(x = population, y = counts, fill = subpopulation)) +
geom_bar(
stat = "identity",
position = "fill"
) +
facet_grid(
~group,
scales = "free",
space = "free"
)
plot_b <-
ggplot(sample_df, aes(x = population, y = counts, fill = subpopulation)) +
geom_bar(
stat = "identity",
position = "fill"
)
plot_a/plot_b
# Plot a/b above, with strip.placement = outside added
plot_c <-
ggplot(
sample_df,
aes(x = population, y = counts, fill = subpopulation)
) +
geom_bar(
stat = "identity",
position = "fill"
) +
facet_grid(
~ group,
scales = "free",
space = "free",
) +
theme(
strip.placement = "outside"
)
plot_d <-
ggplot(
sample_df,
aes(x = population, y = counts, fill = subpopulation)
) +
geom_bar(
stat = "identity",
position = "fill"
)
plot_c/plot_d
A summary of my OS and package versions is below.
devtools::session_info()
─ Session info ─────────────────────────────── setting value version R version 4.3.2 (2023-10-31) os macOS Sonoma 14.2.1 system aarch64, darwin20 ui RStudio language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz America/Denver date 2024-04-22 rstudio 2023.12.1+402 Ocean Storm (desktop) pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
─ Packages ─────────────────────────────────── ! package * version date (UTC) lib source abind 1.4-5 2016-07-21 [1] CRAN (R 4.3.0) backports 1.4.1 2021-12-13 [1] CRAN (R 4.3.0) Biobase * 2.62.0 2023-10-26 [1] Bioconductor BiocGenerics * 0.48.1 2023-11-02 [1] Bioconductor BiocParallel 1.36.0 2023-10-26 [1] Bioconductor bitops 1.0-7 2021-04-24 [1] CRAN (R 4.3.0) blme 1.0-5 2021-01-05 [1] CRAN (R 4.3.0) boot 1.3-28.1 2022-11-22 [1] CRAN (R 4.3.2) broom 1.0.5 2023-06-09 [1] CRAN (R 4.3.0) cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.0) car 3.1-2 2023-03-30 [1] CRAN (R 4.3.0) carData 3.0-5 2022-01-06 [1] CRAN (R 4.3.0) cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.3.0) cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.1) cluster 2.1.6 2023-12-01 [1] CRAN (R 4.3.1) codetools 0.2-19 2023-02-01 [1] CRAN (R 4.3.2) colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0) concaveman 1.1.0 2020-05-11 [1] CRAN (R 4.3.0) cowplot * 1.1.2 2023-12-15 [1] CRAN (R 4.3.1) crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0) data.table 1.15.4 2024-03-30 [1] CRAN (R 4.3.1) DelayedArray 0.28.0 2023-11-06 [1] Bioconductor deldir 2.0-2 2023-11-23 [1] CRAN (R 4.3.1) DESeq2 * 1.42.0 2023-10-26 [1] Bioconductor devtools 2.4.5 2022-10-11 [1] CRAN (R 4.3.0) digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.0) dotCall64 1.1-1 2023-11-28 [1] CRAN (R 4.3.1) dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.1) edgeR 4.0.14 2024-01-30 [1] Bioconductor 3.18 (R 4.3.2) ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.0) entropy 1.3.1 2021-10-02 [1] CRAN (R 4.3.0) evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.1) fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.1) farver 2.1.1 2022-07-06 [1] CRAN (R 4.3.0) fastDummies 1.7.3 2023-07-06 [1] CRAN (R 4.3.0) fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) fitdistrplus 1.1-11 2023-04-25 [1] CRAN (R 4.3.0) forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0) fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.0) future 1.33.1 2023-12-22 [1] CRAN (R 4.3.1) future.apply 1.11.1 2023-12-21 [1] CRAN (R 4.3.1) generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) GenomeInfoDb * 1.38.5 2023-12-30 [1] Bioconductor 3.18 (R 4.3.2) GenomeInfoDbData 1.2.11 2024-01-09 [1] Bioconductor GenomicRanges * 1.54.1 2023-10-30 [1] Bioconductor ggforce 0.4.1 2022-10-04 [1] CRAN (R 4.3.0) ggplot2 * 3.5.0 2024-02-23 [1] CRAN (R 4.3.1) ggplotify * 0.1.2 2023-08-09 [1] CRAN (R 4.3.0) ggpubr * 0.6.0 2023-02-10 [1] CRAN (R 4.3.0) ggrepel 0.9.4 2023-10-13 [1] CRAN (R 4.3.1) ggridges 0.5.5 2023-12-15 [1] CRAN (R 4.3.1) ggsignif 0.6.4 2022-10-13 [1] CRAN (R 4.3.0) ggthemes * 5.0.0 2023-11-21 [1] CRAN (R 4.3.1) glmmTMB 1.1.8 2023-10-07 [1] CRAN (R 4.3.1) globals 0.16.2 2022-11-21 [1] CRAN (R 4.3.0) glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.1) goftest 1.2-3 2021-10-07 [1] CRAN (R 4.3.0) gridExtra 2.3 2017-09-09 [1] CRAN (R 4.3.0) gridGraphics 0.5-1 2020-12-13 [1] CRAN (R 4.3.0) gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.0) HDF5Array 1.30.0 2023-11-06 [1] Bioconductor hexbin 1.28.3 2023-03-21 [1] CRAN (R 4.3.0) hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.0) V htmltools 0.5.7 2024-04-04 [1] CRAN (R 4.3.1) (on disk 0.5.8.1) htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.3.1) httpuv 1.6.13 2023-12-06 [1] CRAN (R 4.3.1) httr 1.4.7 2023-08-15 [1] CRAN (R 4.3.0) ica 1.0-3 2022-07-08 [1] CRAN (R 4.3.0) igraph 1.6.0 2023-12-11 [1] CRAN (R 4.3.1) IRanges * 2.36.0 2023-10-26 [1] Bioconductor irlba 2.3.5.1 2022-10-03 [1] CRAN (R 4.3.0) jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.3.1) KernSmooth 2.23-22 2023-07-10 [1] CRAN (R 4.3.2) knitr 1.45 2023-10-30 [1] CRAN (R 4.3.1) labeling 0.4.3 2023-08-29 [1] CRAN (R 4.3.0) later 1.3.2 2023-12-06 [1] CRAN (R 4.3.1) lattice 0.22-5 2023-10-24 [1] CRAN (R 4.3.1) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.3.0) leiden 0.4.3.1 2023-11-17 [1] CRAN (R 4.3.1) Libra * 1.0.0 2024-02-06 [1] Github (neurorestore/Libra@88f2976) lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.1) limma 3.58.1 2023-11-02 [1] Bioconductor listenv 0.9.0 2022-12-16 [1] CRAN (R 4.3.0) lme4 1.1-35.1 2023-11-05 [1] CRAN (R 4.3.1) lmerTest 3.1-3 2020-10-23 [1] CRAN (R 4.3.0) lmtest 0.9-40 2022-03-21 [1] CRAN (R 4.3.0) locfit 1.5-9.8 2023-06-11 [1] CRAN (R 4.3.0) lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.1) magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) MASS 7.3-60 2023-05-04 [1] CRAN (R 4.3.2) Matrix 1.6-4 2023-11-30 [1] CRAN (R 4.3.1) MatrixGenerics * 1.14.0 2023-10-26 [1] Bioconductor matrixStats * 1.2.0 2023-12-11 [1] CRAN (R 4.3.1) memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.0) mgcv 1.9-1 2023-12-21 [1] CRAN (R 4.3.1) mime 0.12 2021-09-28 [1] CRAN (R 4.3.0) miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.0) minqa 1.2.6 2023-09-11 [1] CRAN (R 4.3.0) munsell 0.5.1 2024-04-01 [1] CRAN (R 4.3.1) nlme 3.1-164 2023-11-27 [1] CRAN (R 4.3.1) nloptr 2.0.3 2022-05-26 [1] CRAN (R 4.3.0) numDeriv 2016.8-1.1 2019-06-06 [1] CRAN (R 4.3.0) parallelly 1.36.0 2023-05-26 [1] CRAN (R 4.3.0) patchwork * 1.2.0 2024-01-08 [1] CRAN (R 4.3.1) pbapply 1.7-2 2023-06-27 [1] CRAN (R 4.3.0) pbmcapply 1.5.1 2022-04-28 [1] CRAN (R 4.3.0) pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) pkgbuild 1.4.3 2023-12-10 [1] CRAN (R 4.3.1) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) pkgload 1.3.3 2023-09-22 [1] CRAN (R 4.3.1) plotly 4.10.3 2023-10-21 [1] CRAN (R 4.3.1) plyr 1.8.9 2023-10-02 [1] CRAN (R 4.3.1) png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0) polyclip 1.10-6 2023-09-27 [1] CRAN (R 4.3.1) profvis 0.3.8 2023-05-02 [1] CRAN (R 4.3.0) progressr 0.14.0 2023-08-10 [1] CRAN (R 4.3.0) promises 1.2.1 2023-08-10 [1] CRAN (R 4.3.0) purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) RANN 2.6.1 2019-01-08 [1] CRAN (R 4.3.0) RColorBrewer * 1.1-3 2022-04-03 [1] CRAN (R 4.3.0) Rcpp 1.0.11 2023-07-06 [1] CRAN (R 4.3.0) RcppAnnoy 0.0.21 2023-07-02 [1] CRAN (R 4.3.0) RcppHNSW 0.5.0 2023-09-19 [1] CRAN (R 4.3.1) RCurl 1.98-1.14 2024-01-09 [1] CRAN (R 4.3.2) readr * 2.1.5 2024-01-10 [1] CRAN (R 4.3.1) readxl * 1.4.3 2023-07-06 [1] CRAN (R 4.3.0) remotes 2.4.2.1 2023-07-18 [1] CRAN (R 4.3.0) reshape2 1.4.4 2020-04-09 [1] CRAN (R 4.3.0) reticulate 1.34.0 2023-10-12 [1] CRAN (R 4.3.1) rhdf5 2.46.1 2023-12-02 [1] Bioconductor 3.18 (R 4.3.2) rhdf5filters 1.14.1 2023-12-16 [1] Bioconductor 3.18 (R 4.3.2) Rhdf5lib 1.24.1 2023-12-12 [1] Bioconductor 3.18 (R 4.3.2) rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.1) rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.1) ROCR 1.0-11 2020-05-02 [1] CRAN (R 4.3.0) RSpectra 0.16-1 2022-04-24 [1] CRAN (R 4.3.0) rstatix 0.7.2 2023-02-01 [1] CRAN (R 4.3.0) rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.0) Rtsne 0.17 2023-12-07 [1] CRAN (R 4.3.1) S4Arrays 1.2.0 2023-10-26 [1] Bioconductor S4Vectors * 0.40.2 2023-11-25 [1] Bioconductor 3.18 (R 4.3.2) scales * 1.3.0 2023-11-28 [1] CRAN (R 4.3.1) scattermore 1.2 2023-06-12 [1] CRAN (R 4.3.0) schex * 1.16.1 2024-02-03 [1] Bioconductor 3.18 (R 4.3.2) sctransform 0.4.1 2023-10-19 [1] CRAN (R 4.3.1) SCUBA 0.9.0 2024-01-16 [1] Github (amc-heme/SCUBA@d8fbdd9) sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) Seurat * 5.0.1 2023-11-17 [1] CRAN (R 4.3.1) SeuratObject * 5.0.1 2023-11-17 [1] CRAN (R 4.3.1) shiny 1.8.0 2023-11-17 [1] CRAN (R 4.3.1) SingleCellExperiment * 1.24.0 2023-11-06 [1] Bioconductor sp * 2.1-2 2023-11-26 [1] CRAN (R 4.3.1) spam 2.10-0 2023-10-23 [1] CRAN (R 4.3.1) SparseArray 1.2.3 2023-12-26 [1] Bioconductor 3.18 (R 4.3.2) spatstat.data 3.0-3 2023-10-24 [1] CRAN (R 4.3.1) spatstat.explore 3.2-5 2023-10-22 [1] CRAN (R 4.3.1) spatstat.geom 3.2-7 2023-10-20 [1] CRAN (R 4.3.1) spatstat.random 3.2-2 2023-11-29 [1] CRAN (R 4.3.1) spatstat.sparse 3.0-3 2023-10-24 [1] CRAN (R 4.3.1) spatstat.utils 3.0-4 2023-10-24 [1] CRAN (R 4.3.1) statmod 1.5.0 2023-01-06 [1] CRAN (R 4.3.0) stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.1) stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.1) SummarizedExperiment * 1.32.0 2023-11-06 [1] Bioconductor survival 3.5-7 2023-08-14 [1] CRAN (R 4.3.2) svglite * 2.1.3 2023-12-08 [1] CRAN (R 4.3.1) systemfonts 1.0.5 2023-10-09 [1] CRAN (R 4.3.1) tensor 1.5 2012-05-05 [1] CRAN (R 4.3.0) tester 0.1.7 2013-11-14 [1] CRAN (R 4.3.0) tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.3.1) tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.3.1) tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0) timechange 0.3.0 2024-01-18 [1] CRAN (R 4.3.1) tinytex * 0.49 2023-11-22 [1] CRAN (R 4.3.1) TMB 1.9.10 2023-12-12 [1] CRAN (R 4.3.1) tweenr 2.0.2 2022-09-06 [1] CRAN (R 4.3.0) tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.0) urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.3.0) usethis 2.2.2 2023-07-06 [1] CRAN (R 4.3.0) utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.1) uwot 0.1.16 2023-06-29 [1] CRAN (R 4.3.0) vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.1) viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.3.0) withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.1) xfun 0.41 2023-11-01 [1] CRAN (R 4.3.1) xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.0) XVector 0.42.0 2023-10-26 [1] Bioconductor yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.1) yulab.utils 0.1.4 2024-01-28 [1] CRAN (R 4.3.1) zlibbioc 1.48.0 2023-10-26 [1] Bioconductor zoo 1.8-12 2023-04-13 [1] CRAN (R 4.3.0)
[1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
V ── Loaded and on-disk version mismatch.
──────────────────────────────────────────────
More minimal example. Note misalignment of bottom axis of the left plot.
library(ggplot2)
library(patchwork)
p0 <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
p1 <- p0 +
facet_wrap(~"foo") +
theme(strip.placement = "outside")
p0 + p1

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