I have a function DisplayPlot which takes as an input a list of strings (which refers to columns in a dataframe). For instance, if list_string = c("string1","string2"), looping on the elements of list_string, I would like to display the two ggplots side-by-side.
So far what I have done in my function is storing the ggplots into a list:
for (i in 1:length(list_string){
p = ggplot(data=d, aes(x=d[,1], y=as.numeric(levels(d[,(i+1)]))[d[,(i+1)]], fill=d[,1])) +
geom_bar(stat="identity") +
guides(fill=FALSE) +
labs(x = Y) + labs(y = paste("CP",list_X[i],"_per_",Y,sep = ""))
graph[[length(graph) + 1]] <- p }
and return:
return(list(graph=graph, along with some other information))
The issue is that if I want to use a function like grid.arrange
, I need to write manually the names of the plots: grid.arrange(p1,p2,ncol=2)
whereas in my case, this number may vary and are stored into a list (format that grid.arrange
does not like).
Thank you very much in advance for your help, Clement