r - Issue: ggplot2 replicates last plot of a list in grid -


i have 16 plots. want plot of these in grid manner ggplot2. but, whenever plot, grid plots same, i.e, last plot saved in list gets plotted @ 16 places of grid. replicate same issue, here providing simple example 2 files. although data entirely different, plots drawn similar.

library(ggplot2) library(grid) library(gridextra) library(scales)  set.seed(1006) date1<- as.posixct(seq(from=1443709107,by=3600,to=1446214707),origin="1970-01-01") power <- rnorm(length(date1),100,5)#with normal distribution write.csv(data.frame(date1,power),"file1.csv",row.names = false,quote = false) # dataset uniform distribution write.csv(data.frame(date1,power=runif(length(date1))),"file2.csv",row.names = false,quote = false)     path=getwd()     files=list.files(path,pattern="*.csv")     plist<-list()# saving intermediate ggplots     for(i in 1:length(files))       {       dframe<-read.csv(paste(path,"/",files[i],sep = ""),head=true,sep=",")       dframe$date1= as.posixct(dframe$date1)       plist[[i]]<- ggplot(dframe)+aes(dframe$date1,dframe$power)+geom_line()       }     grid.arrange(plist[[1]],plist[[2]],ncol = 1,nrow=2) 

you need remove dframe call aes. should anyway because have provided data-argument. in case it's more important because while save ggplot-object, things don't evaluated until call plot/grid.arrange. when that, looks @ current value of dframe, last dataset in iteration.

you need plot with:

ggplot(dframe)+aes(date1,power)+geom_line() 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -