0

until now i am stuck at looping and saving a ggplot from List i have looked at the another question but it did not working.

myplots=list()

par(mfrow = c(1, 5))
for (i in 1:5) { 
  #name=paste("ggp", i, sep = "_")
  
  p1 =ggplot(Turbine[[i]],
                          aes(x=Turbine[[i]]$TS,
                          y=Turbine[[i]]$Pv..turbine))+
                          geom_point(size=1)+
                          ggtitle(names(Turbine[i])
                          )
  print(i)
  print(p1)
  myplots[[i]]= p1
}
 
multiplot(plotlist=myplots,cols=5)
plot_grid(ggp_1,ggp_2,ggp_3,ggp_4,ggp_5) #trying to save ggplot as variable name

the problem i got is when i want to start plot of multiple plot in 1 drawing. i want to have 5 column of plots.

maybe s lapply func. is good?

let the data be

Turbine=list of listname
first listname= name(Turbine[1])
view(Turbine [[1]]) 
TS Pv..turbine
1   20
2   20
3   24
4   19
   

so

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Does this answer your question? [Store multiple plots into a list using a function](https://stackoverflow.com/questions/59960505/store-multiple-plots-into-a-list-using-a-function) Afterwards you can go on with cowplot::plot_grid or make use of patchwork::wrap_plots which takes a list as argument – stefan Jan 26 '21 at 18:34

1 Answers1

1

Create something like your list:

Turbine = lapply(1:5,data.frame(TS=1:10,"Pv..turbine"=runif(10))

You can use the plotlist= argument in plot_grid, note, you don't need par(mfrow=..)), thats meant for base R plots and also you don't need to use the $ inside aes :

library(cowplot)
library(ggplot2)

myplots=list()

for (i in 1:5) { 
   myplots[[i]] = ggplot(Turbine[[i]],
              aes(x=TS,y=Pv..turbine))+
              geom_point(size=1)
}
 
plot_grid(plotlist=myplots,ncol=5) 
StupidWolf
  • 45,075
  • 17
  • 40
  • 72