I'm trying to workout a function (without any success) that will automatically assemble a list of ggplots plots with patchwork
.
The function would take in the following inputs:
- no_of_rows
- no_of_cols
- master_list_with_plots
With thee inputs the function should automatically structure the plots. I have provided an example below:
library(tidyverse)
library(patchwork)
p1 <- mtcars %>% ggplot() + geom_bar(aes(mpg))
p2 <- mtcars %>% ggplot() + geom_bar(aes(cyl))
p3 <- mtcars %>% ggplot() + geom_bar(aes(disp))
p4 <- mtcars %>% ggplot() + geom_bar(aes(hp))
p5 <- mtcars %>% ggplot() + geom_bar(aes(drat))
p6 <- mtcars %>% ggplot() + geom_bar(aes(wt))
plot_list <- list(mpg = p1,
cyl = p2,
disp = p3,
hp = p4,
drat = p5,
wt = p6)
# Function inputs
no_of_rows = 2
no_of_col2 = 3
master_list_with_plots = org_plot
#Function output would be org_plot below
org_plot <- plot_list[["mpg"]] + plot_list[["cyl"]] + plot_list[["disp"]] +
plot_list[["hp"]] + plot_list[["drat"]] + plot_list[["wt"]]
Any ideas?