patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Can't combine align_patches with layout

Open bwiernik opened this issue 3 years ago • 7 comments

I am trying to ensure that several patchworks have the same dimensions for their plots. However, I get errors when I try to align the dimensions, then include the resulting plots in a patchwork:

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

plot_dim <- get_max_dim(list(p1, p2, p3, p4))

p1 <- set_dim(p1, plot_dim)
p2 <- set_dim(p2, plot_dim)
p3 <- set_dim(p3, plot_dim)
p4 <- set_dim(p4, plot_dim)

# Errors
p1 + p2 + p3 + p4

# Errors 
(p1 | p2) / (p3 | p4)

bwiernik avatar Aug 26 '20 02:08 bwiernik

Yeah. These are not meant to be used together. align_patches uses the same alignment as the main patchwork functionality so nothing is gained by trying to use them together

thomasp85 avatar Aug 26 '20 06:08 thomasp85

Is there any way to ensure the same alignment across multiple patchworks? The use case I have is I have a bunch of survival curves that I want to show in groups. Each group has a descriptive subtitle. I'd like the width of the guides and the axes to be aligned across all of the plots regardless of group.

bwiernik avatar Aug 26 '20 10:08 bwiernik

Ah - now I see what you want... I don't think that is possible right now. I'll consider how to add that in the future

thomasp85 avatar Aug 26 '20 10:08 thomasp85

Thanks

bwiernik avatar Aug 26 '20 11:08 bwiernik

I came here to add my support for requesting this feature. My use case is identical to @bwiernik's (except I'm plotting scatter plots rather than survival curves). Looks like #254 is requesting also. Adding another reprex to illustrate lack of alignment across patches:

library(ggplot2)
library(patchwork)

plot_list <- lapply(
  X = 1:12,
  FUN = \(i) {
    ggplot(pressure, aes(temperature, pressure)) +
      geom_point()
  }
)

plot_groups <- list(1:6, 7, 8:10, 11:12)

layout <- c(
  area(1, 1, 1, 1),
  area(1, 2, 1, 2),
  area(1, 3, 1, 3),
  area(2, 1, 2, 1),
  area(2, 2, 2, 2),
  area(2, 3, 2, 3)
)

patchworks <- lapply(
  X = seq_along(plot_groups),
  FUN = \(i) {
    title <- paste("Plot Group", i)
    plots <- plot_list[plot_groups[[i]]]
    plots |> 
      wrap_plots(design = layout) +
      plot_annotation(title = title)
  }
)

invisible(lapply(patchworks, print))

caparks2 avatar Sep 29 '23 07:09 caparks2

Actually, functionality to perform alignment and composition separately already exists in cowplot, so maybe it's not a necessary feature for patchwork.

library(ggplot2)
library(cowplot)

all_plots <- lapply(
  X = 1:12,
  FUN = \(i) {
    ggplot(pressure, aes(temperature, pressure)) +
      geom_point()
  }
)

plot_groups <- list(1:6, 7, 8:10, 11:12)

all_aligned_plots <- cowplot::align_plots(
  plotlist = all_plots,
  align = "hv", 
  axis = "tblr",
  greedy = TRUE
)

cowplots <- lapply(
  X = seq_along(plot_groups),
  FUN = \(i) {
    aligned_plots <- all_aligned_plots[plot_groups[[i]]]
    
    cowplot <- 
      cowplot::plot_grid(
        plotlist = aligned_plots,
        nrow = 2,
        ncol = 3
      ) 
    
    title <- 
      cowplot::ggdraw() + 
      cowplot::draw_label(
        label = paste("Plot Group", i),
        x = 0,
        hjust = 0
      ) +
      theme(plot.margin = margin(0, 0, 0, 7))
    
    cowplot::plot_grid(
      plotlist = list(title, cowplot),
      ncol = 1,
      rel_heights = c(0.1, 1)
    )
  }
)

invisible(lapply(cowplots, print))

caparks2 avatar Oct 03 '23 15:10 caparks2

I'm really not interested in using cowplot, so would really appreciate if this could be added to patchwork!

bwiernik avatar Oct 03 '23 16:10 bwiernik