Unable to render plot when combining paginated plots with empty facets
Background: I am working with several faceted plots which I need to split into multiple slides to avoid overplotting, while ensuring that facets/subplots from the same group (i.e., the facetting variables) are aligned in parallel. To accomplish this, I used ggforce::facet_wrap_paginate to draw the contents of each page and stitch them together using patchwork grammar within a loop. This method works perfectly when the total number of facets is a multiple of the facets per page. However, it fails to render the final page properly when the number of facets is not divisible by the facets per page. It created a new page but draw nothing.
A similar issue was reported in #273, which has been marked as resolved at commit 94f04c42f2e8f90456b0f063e3a93336d17485db. I am currently using the latest versions of patchwork and ggforce available through Conda.
Minimal reprex:
library(ggplot2)
library(ggforce)
library(patchwork)
p1_1 <- ggplot(diamonds, mapping = aes(x = carat, y = price)) +
geom_point() +
facet_wrap_paginate(vars(cut), nrow = 1, ncol = 3, page = 1) +
theme_classic()
p2_1 <- ggplot(diamonds, mapping = aes(x = x, y = price)) +
geom_point() +
facet_wrap_paginate(vars(cut), nrow = 1, ncol = 3, page = 1) +
xlab("length") +
theme_classic()
p1_1 / p2_1
p1_2 <- ggplot(diamonds, mapping = aes(x = carat, y = price)) +
geom_point() +
facet_wrap_paginate(vars(cut), nrow = 1, ncol = 3, page = 2) +
theme_classic()
p2_2 <- ggplot(diamonds, mapping = aes(x = x, y = price)) +
geom_point() +
facet_wrap_paginate(vars(cut), nrow = 1, ncol = 3, page = 2) +
xlab("length") +
theme_classic()
p1_2 / p2_2
No plot was generated here.
GridExtra has no issue combining the plot (yet unable to align the y-axis properly in my case).
gridExtra::grid.arrange(p1_2, p2_2, nrow = 2)
Created on 2025-04-25 with reprex v2.1.1
Session info
sessionInfo()
#> Warning: Your system is mis-configured: '/etc/localtime' is not a symlink
#> Warning: '/etc/localtime' is not identical to any known timezone file
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-conda-linux-gnu
#> Running under: CentOS Linux 8
#>
#> Matrix products: default
#> BLAS/LAPACK: <conda_prefix>/lib/libopenblasp-r0.3.29.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: NA
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] patchwork_1.3.0 ggforce_0.4.2 ggplot2_3.5.1
#>
#> loaded via a namespace (and not attached):
#> [1] vctrs_0.6.5 cli_3.6.4 knitr_1.50 rlang_1.1.5
#> [5] xfun_0.51 generics_0.1.3 labeling_0.4.3 glue_1.8.0
#> [9] colorspace_2.1-1 htmltools_0.5.8.1 gridExtra_2.3 scales_1.3.0
#> [13] rmarkdown_2.29 tweenr_2.0.3 grid_4.4.2 evaluate_1.0.3
#> [17] munsell_0.5.1 tibble_3.2.1 MASS_7.3-64 fastmap_1.2.0
#> [21] yaml_2.3.10 lifecycle_1.0.4 compiler_4.4.2 dplyr_1.1.4
#> [25] polyclip_1.10-7 fs_1.6.5 Rcpp_1.0.14 pkgconfig_2.0.3
#> [29] rstudioapi_0.17.1 farver_2.1.2 digest_0.6.37 R6_2.6.1
#> [33] tidyselect_1.2.1 reprex_2.1.1 pillar_1.10.1 magrittr_2.0.3
#> [37] tools_4.4.2 withr_3.0.2 gtable_0.3.6