Cowplot `plot_grid` producing (far) too much whitespace when ggplot aspect ratio is changed

This code produces the cowplot grid shown below.

library(tidyverse)
library(cowplot)


plot_1 <- tibble(x = rnorm(25),
                 y = rnorm(25)) %>% 
  ggplot(aes(x = x, y = y)) + 
  geom_point() 

plot_grid(plot_1, plot_1, labels=c('a', 'b'))

But let's say I want to change the aspect ratio. Easily done, or so I thought, with ggplot's theme.

plot_2 <- plot_1 + theme(aspect.ratio = 1/2)

plot_grid(plot_2, plot_2, labels=c('a', 'b'))

But the result is this.

I also tried

plot_3 <- plot_1 + coord_fixed(ratio=1/2)
plot_grid(plot_3, plot_3, labels=c('a', 'b')) 

But the results are the same.

The labels are where they were in the original. I want them plots to be just under the labels and none of the whitespace.

I have encountered the same problem! I’m not sure if it’s a bug, or expected behavior (I guess the best way to answer that might be to post a good reproducible example β€” like yours! :smile: β€” to the github issues).

My workaround was to stop setting aspect ratio via ggplot2, and instead set it elsewhere: within the chunk options (for R Markdown) or via cowplot::save_plot (for general output to file).

1 Like

Thanks for your help. I will start an issue at GitHub and hope that that will lead to some insight.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.