Error: Can't add `b` to a ggplot object.
library(patchwork)
library(ggplot2)
data("midwest", package = "ggplot2")
a <- ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity))
b <- ggplot(midwest, aes(x=poptotal, y=area)) +
geom_point(aes(col=state, size=popdensity))
a + b
Error: Can't add `b` to a ggplot object.
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
[3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.1252
attached base packages:
[1] splines stats graphics grDevices datasets utils methods base
other attached packages:
[1] patchwork_2.4 yardstick_0.0.8 workflowsets_0.0.2 workflows_0.2.2
[5] tune_0.1.5 tidyr_1.1.3 tibble_3.1.1 rsample_0.0.9
[9] recipes_0.1.16 purrr_0.3.4 parsnip_0.1.5 infer_0.5.4
[13] ggplot2_3.3.3 dplyr_1.0.6 dials_0.0.9 scales_1.1.1
[17] broom_0.7.6 tidymodels_0.1.3 modeldata_0.1.0
loaded via a namespace (and not attached):
[1] foreach_1.5.1 prodlim_2019.11.13 assertthat_0.2.1 conflicted_1.0.4
[5] BiocManager_1.30.12 GPfit_1.0-8 renv_0.13.0 globals_0.14.0
[9] ipred_0.9-11 pillar_1.6.0 backports_1.2.1 lattice_0.20-41
[13] glue_1.4.2 pROC_1.17.0.1 digest_0.6.27 colorspace_2.0-1
[17] Matrix_1.2-18 plyr_1.8.6 timeDate_3043.102 pkgconfig_2.0.3
[21] lhs_1.1.1 DiceDesign_1.9 listenv_0.8.0 gower_0.2.2
[25] lava_1.6.9 mgcv_1.8-33 generics_0.1.0 farver_2.1.0
[29] ellipsis_0.3.2 cachem_1.0.4 withr_2.4.2 furrr_0.2.2
[33] nnet_7.3-14 cli_2.5.0 survival_3.2-7 magrittr_2.0.1
[37] crayon_1.4.1 future_1.21.0 fansi_0.4.2 parallelly_1.25.0
[41] nlme_3.1-149 MASS_7.3-53 class_7.3-17 tools_4.0.3
[45] lifecycle_1.0.0 munsell_0.5.0 compiler_4.0.3 rlang_0.4.11
[49] grid_4.0.3 iterators_1.0.13 rstudioapi_0.13 labeling_0.4.2
[53] gtable_0.3.0 codetools_0.2-16 DBI_1.1.1 R6_2.5.0
[57] lubridate_1.7.10 fastmap_1.1.0 utf8_1.2.1 parallel_4.0.3
[61] Rcpp_1.0.6 vctrs_0.3.8 rpart_4.1-15 tidyselect_1.1.1
Running the exact same code in R 4.1 + the newest Rstudio version
creates this for me without any problem

It seems that you have somehow a wrong patchwork package installed.
The current version is patchwork_1.1.1 and patchwork_2.4 does not exist yet. I suggest that you install patchwork again from cran. Eg:
install.packages("patchwork",repos = "https://cloud.r-project.org")
I had the same issue, caused by there being two packages both called patchwork...
Installing via install.packages("patchwork", repos = "https://cloud.r-project.org") solved the problem for me.
I had a look through the results of options('repos') and it looks like the patchwork listed at https://r-forge.r-project.org/projects/patchwork/ is incorrect (this version of patchwork offers "Allele-specific Copy-number analysis for whole-genome sequenced data in CompleteGenomics or BAM format").
I had the same issue, caused by there being two packages both called
patchwork...Installing via
install.packages("patchwork", repos = "https://cloud.r-project.org")solved the problem for me.I had a look through the results of
options('repos')and it looks like thepatchworklisted at https://r-forge.r-project.org/projects/patchwork/ is incorrect (this version ofpatchworkoffers "Allele-specific Copy-number analysis for whole-genome sequenced data in CompleteGenomics or BAM format").
Great