Grid of Multiple Ggplot2 Plots Which Have Been Made in a for Loop

Grid of multiple ggplot2 plots which have been made in a for loop

I would be inclined to agree with Richie, but if you want to arrange them yourself:

library(gridExtra)
library(ggplot2)
p <- list()
for(i in 1:4){
p[[i]] <- qplot(1:10,10:1,main=i)
}
do.call(grid.arrange,p)

take a look at the examples at the end of ?arrangeGrob for ways to eliminate the for loop altogether:

plots = lapply(1:5, function(.x) qplot(1:10,rnorm(10),main=paste("plot",.x)))
require(gridExtra)
do.call(grid.arrange, plots)

How to save multiple ggplot charts in loop for using grid.arrange

This could be solved by initiating a list to store the plot objects instead of vector

p <- vector('list', N)
for(i in seq_len(N)) {
p[[i]] <- ggplot(...)
}

grid.arrange(p[[1]], p[[2]], ..., p[[N]], nrow = 4)

How to generate multiple ggplots using a for loop

Try with this:

library(ggplot2)
#Function does not return graph
for (i in list){
var <- sym(i)
print(ggplot(data = test_df, aes(x= DateTime.lub, y = !!var))+
geom_line(aes(colour = Step))+
ggtitle(paste0('plot_',i)))
}


Related Topics



Leave a reply



Submit