Size of figures in knitr with patchwork or cowplot

1k views Asked by At

I have a large number of figures which I would like to present in a single column in a document created with knitr. The number of figures can vary, so I created a function to combine all figures using patchwork::wrap_plots (cowplot::plot_grid is an alternative, but has the same issues described below). MWE:

library(patchwork)
library(tidyverse)

plots <- lapply(1:10, function(x) data.frame(A = rnorm(1:100, 0, 1), B = rnorm(1:100, 0, 1)) %>% 
                  ggplot(., aes(x = A, y = B)) + 
                  geom_point() +
                  coord_equal())

wrap_plots(plots, ncol = 1)

By default, this creates tiny plots. (Leaving out coord_equal leads to plots of the same height, but with full page width):

Plot with patchword::wrap_plots

Using knitr's chunk option fig.height (but not fig.width) it is possible to scale up the plots. However, this leads to very large margins above and below the entire plot.

I have found a number of solution for controlling the size of patchwork or cowplot plots (e.g., using cowplot::save_plot). However, none of these seem applicable to documents created with knitr.

Preferably, I would like figures to automatically have the same width as text in the knitr output, without excessive margins.

0

There are 0 answers