Reputation: 15
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
Upvotes: 0
Views: 666
Reputation: 46988
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)
Upvotes: 1