Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't combine align_patches with layout #207

Open
bwiernik opened this issue Aug 26, 2020 · 7 comments
Open

Can't combine align_patches with layout #207

bwiernik opened this issue Aug 26, 2020 · 7 comments
Labels
feature a feature request or enhancement

Comments

@bwiernik
Copy link
Contributor

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)
@thomasp85
Copy link
Owner

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

@bwiernik
Copy link
Contributor Author

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.

@thomasp85
Copy link
Owner

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

@bwiernik
Copy link
Contributor Author

Thanks

@thomasp85 thomasp85 added the feature a feature request or enhancement label Aug 8, 2023
@caparks2
Copy link

caparks2 commented Sep 29, 2023

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
Copy link

caparks2 commented Oct 3, 2023

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))

@bwiernik
Copy link
Contributor Author

bwiernik commented Oct 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants