Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

ggplot2 - How to create multiple plot from a list which contain List of dataframe in R

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

question from:https://stackoverflow.com/questions/65903473/how-to-create-multiple-plot-from-a-list-which-contain-list-of-dataframe-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...