patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Issue with combining plots when aspect ratio is 1

Open mhovd opened this issue 3 years ago • 1 comments

As asked on Stack Overflow, I have encountered an issue with combinig plots using patchwork when the aspect ratio is 1 for all plots.

library(patchwork)

# Create the base plots
plotlist = list(
  fig1 = iris %>% 
    filter(Species == "setosa") %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point(),
  
  fig2 = iris %>% 
    filter(Species == "versicolor") %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point(),
  
  fig3 = iris %>% 
    filter(Species == "virginica") %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()
)

Here the patchwork combines the plots nicely plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)

image

However, when changing the aspect ratio, something happens:

plotlist = lapply(plotlist, function(x) {
  x + theme(aspect.ratio = 1)
})

Notice the large gap between plots. Instead, I would like the plots in the second row to be almost directly under the first row. plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)

image

Any recommended solutions?

mhovd avatar Jul 17 '21 10:07 mhovd

I have written a solution for you on stack overflow that seems to produce your desired result. But in the process of writing the solution, I did uncover another bug which I have opened in #274. Cheers.

ltuijnder avatar Aug 26 '21 07:08 ltuijnder