0

I am trying to create a list of plots by iteratively selecting data.frames from a list I created previously; however, I get an error back whenever I try selecting an element of that list of plots other than the last one:

Data: a list of 4 different data frames 'IT' (see bottom for an str)

Code:

for(i in 1:length(IT)){
  myplotlist[[i]]<- ggplot(IT[[i]],aes(IT[[i]][,1],IT[[i]][,2]))+geom_point()
}

Problem: When i select myplotlist[[1]] i get back:

Error: Aesthetics must be either length 1 or the same as the data (370): x, y

However, myplotlist[[4]] gives me the correct plot.

Anyone has an idea here? I believe the for loop can be easily omitted by using lapply.

Thanks a lot,

str(IT)

    'IT' is a list of 4 

    $ :'data.frame':    370 obs. of  3 variables:
     ..$ AsofDate                      : Date[1:370], format: "2014-04-08" "2014-           04-09" "2014-04-17" "2014-05-07" ...
      ..$ FUT_CLOSE_BASE_Q1: num [1:370] 55.5 55.5 56.9 56 56.6 ...
  ..$ Q                             : chr [1:370] "Q1" "Q1" "Q1" "Q1" ...

 $ :'data.frame':   288 obs. of  3 variables:
  ..$ AsofDate                      : Date[1:288], format: "2014-04-07" "2014-05-19" "2014-07-14" "2014-07-15" ...
  ..$ FUT_CLOSE_BASE_Q2: num [1:288] 47.3 47.6 47.6 47.6 48.3 ...
  ..$ Q                             : chr [1:288] "Q2" "Q2" "Q2" "Q2" ...

 $ :'data.frame':   375 obs. of  3 variables:
  ..$ AsofDate                      : Date[1:375], format: "2014-04-07" "2014-04-08" "2014-04-09" "2014-04-10" ...
 ..$ FUT_CLOSE_BASE_Q3: num [1:375] 53.9 53.1 53.5 53 53.6 ...
  ..$ Q                             : chr [1:375] "Q3" "Q3" "Q3" "Q3" ...

     $ :'data.frame':   467 obs. of  3 variables:
      ..$ AsofDate                      : Date[1:467], format: "2014-04-07"     "2014-04-08" "2014-04-09" "2014-04-10" ...
      ..$ FUT_CLOSE_BASE_Q4: num [1:467] 56.8 56.5 56.5 56.3 57.2 ...
      ..$ Q                             : chr [1:467] "Q4" "Q4" "Q4" "Q4" ...
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • Please look at [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on creating a reproducible example. – zoneparser Dec 07 '16 at 12:02
  • solved it by adding grid.arrange(p1) `require(gridExtra) myplotlist=list() for(i in 1:length(IT)){ p1 = ggplot(IT[[i]],aes(x=IT[[i]][,1],y=IT[[i]][,2]))+geom_point()+geom_line()+ylab(paste(colnames(IT[[i]][2])))+xlab(paste(colnames(IT[[i]][1]))) myplotlist[[i]]<- grid.arrange(p1) }` ill try to provide some data to reproduce this error – Lippmann Schwinger Dec 07 '16 at 16:25

0 Answers0