Skip to content

fails with ggh4x::facet_wrap2(trim_blank=FALSE) #442

Description

@r2evans

When using ggh4x::facet_wrap2 in a subplot and combining, it works only if trim_blank=TRUE (the default); if I try to preserve blanks then it presents a blank canvas.

library(ggplot2)
library(patchwork)
spldat <- data.frame(x=c(1, 1, 2), y = c(1, 2, 2)) |> split(~ x)
plots <- lapply(spldat, \(dat) ggplot(dat, aes(x, y)) + geom_point() +
                                 ggh4x::facet_wrap2(y ~ ., ncol = 2, trim_blank = TRUE))
Reduce(`/`, plots)
Image
plots <- lapply(spldat, \(dat) ggplot(dat, aes(x, y)) + geom_point() +
                                 ggh4x::facet_wrap2(y ~ ., ncol = 2, trim_blank = FALSE))
Reduce(`/`, plots)

(empty for me)

plots[[1]]
plots[[2]]
Image Image
session info
xfun::session_info("patchwork")
# R version 4.4.3 (2025-02-28)
# Platform: aarch64-apple-darwin20
# Running under: macOS Sequoia 15.5
# Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
# Package version:
#   cli_3.6.5          farver_2.1.2       ggplot2_3.5.2      glue_1.8.0         graphics_4.4.3     grDevices_4.4.3    grid_4.4.3         gtable_0.3.6      
#   isoband_0.2.7      labeling_0.4.3     lattice_0.22.6     lifecycle_1.0.4    magrittr_2.0.3     MASS_7.3.64        Matrix_1.7.2       methods_4.4.3     
#   mgcv_1.9.1         nlme_3.1.167       patchwork_1.3.1    pillar_1.10.2      pkgconfig_2.0.3    R6_2.6.0           RColorBrewer_1.1.3 rlang_1.1.6       
#   scales_1.4.0       splines_4.4.3      stats_4.4.3        tibble_3.3.0       utf8_1.2.4         utils_4.4.3        vctrs_0.6.5        viridisLite_0.4.2 
#   withr_3.0.2       

Activity

  1. teunbrand commented on Aug 11, 2025

    @teunbrand
    Collaborator

    It might be that one of the assumptions of patchwork is to have a grob present (even if it is a zeroGrob()) at a panel position, whereas this is not the case with ggh4x::facet_wrap2(trim_blank = FALSE). It might be more appropriate for ggh4x to replace the empty panels with zeroGrob()s than to remove the panels.

  2. r2evans commented on Aug 11, 2025

    @r2evans
    Author

    I figured it'd be something special with the empty space.

  3. r2evans commented on Apr 8, 2026

    @r2evans
    Author

    Any traction here in patchwork or in ggh4x#202? @teunbrand

  4. teunbrand commented on Apr 8, 2026

    @teunbrand
    Collaborator

    I explored this problem a while ago but haven't found a satisfactory solution. Leaving in dummy grobs to satisfy the gtable assumptions would disrupt the current mechanism too much for me to commit to that.

  5. r2evans commented on Apr 9, 2026

    @r2evans
    Author

    For the record (future me + anybody else who may be interested, until something more "proper" may be implemented), an interim solution for me is to use ggplot2::facet_wrap() for the normal plot, and add another similar plot with the correct number of panes with theme_void() (completely empty). It's imperfect (the pane alignment is off a little in some of my plots though not below), but it's workable for now.

    library(ggplot2)
    library(patchwork)
    want_cols <- 6
    gg2 <- ggplot(mtcars, aes(mpg,disp)) + geom_point() + facet_wrap(vs + gear ~ ., ncol = want_cols)
    gg1a <- ggplot(mtcars, aes(mpg,disp)) + geom_point() + facet_wrap(cyl ~ ., ncol = want_cols)
    (need_cols <- (want_cols - length(unique(ggplot_build(gg1a)$layout$layout$PANEL))))
    # [1] 3
    (facet_vars <- names(gg1a$facet$params$facets))
    # [1] "cyl"
    gg1b <- merge(
      subset(gg1a$data[1,], select = setdiff(names(gg1a$data), facet_vars)),
      as.data.frame(lapply(setNames(nm=facet_vars), function(z) strrep(" ", 1:need_cols))),
      by = NULL
      ) |>
      ggplot() + 
      facet_wrap(vars(!!!lapply(facet_vars, as.symbol)), ncol = want_cols) +
      theme_void()
    (gg1a + gg1b) / gg2
    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions