2

using ggplot2 inside for loop doesn't show the plots names I want to print the plots into one page and save them

I have 60 csv files each consists of two columns 1st is date and second is ssh and each has different rows number. I list them in variable called files and then I plot them. the for loop produced 60 plots. the problem is how to know the name of those plots to call them when I want e.g. to print 4 plots in one page so I will have at the end 15 pages each contains 4 plots. when I used

library(gridExtra)
grid.arrange(p1,p2,p3,p4, nrow=2,ncol=2)
grid.arrange(p5,p6,p7,p8, nrow=2,ncol=2) 

and so on till p60 it showed no result. warning messages said there is no object called p1, p2,p3,p4,....p60.

the code is as follows:

files<- list.files("F:/R Practice/time series")
for (i in seq_along(files)){
  mydf <- read.csv(files[i], stringsAsFactors=FALSE)
  a<- data.frame(as.Date(mydf$date, "%d-%m-%y"),mydf[,-1])
  names(a)[1]<- "Date"
  names(a)[2]<- "SSH"
  b <- zoo(a[,-1],order.by=as.Date(a[,1]))
  p<- ggplot(a, aes(x=Date,y=SSH, color=SSH)) +geom_line(colour="darkblue")# +labs(title = "gridcell of (31.25N, 33.25E) ",x="Date", y="SSH")
  p<-p+ggtitle(readline(prompt = "enter cell coordinates:  "))+xlab("Year")+ylab("weighted average SSH of center cell")
  # adding atrribute to the plot p[i]
  p<-p+theme(axis.title.x=element_text(color = "black",size = 12),
               axis.title.y=element_text(color = "black",size = 8),
               axis.text.x=element_text(color="black",size=8),
               axis.text.y = element_text(color = "black",size = 8),
               panel.background = element_rect(colour = "black", size=0.5 ,fill = NA),
               panel.grid.minor = element_blank(),
               panel.grid.major = element_blank(),
               axis.line = element_line(colour = "black",size=1),
               legend.position="none" ,
           plot.title=element_text(hjust = 0.5,vjust= 0.5,lineheight = .3,face = "bold"))

  print(p)
}
camille
  • 16,432
  • 18
  • 38
  • 60
Jisika
  • 405
  • 3
  • 9
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. We have neither your data nor your output, so we're only guessing. You also seem to be trying to work with objects you never created: maybe you mean `p[1]` instead of `p1`? – camille Apr 24 '19 at 13:17

1 Answers1

3

Your code is overwriting p each time to the last plot. Here is simpler code using the examples from the ggplot2 help example but with a loop like yours.

df <- data.frame(
       gp = factor(rep(letters[1:3], each = 10)),
       y = rnorm(30)
   )

p.list<- list()
for (i in 1:4)
{
  df<- sample_frac(df, 0.5)

  p<- ggplot(df, aes(gp, y)) +
    geom_point() +
    geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)
  p.list[[i]] <-p
}

grid.arrange(p.list[[1]], p.list[[2]], p.list[[3]], p.list[[4]], nrow= 2 )

Note two things:

  1. p.list begins as an empty list.

  2. At the end of each loop you add a plot to a numbered element of a growing list i.e. p.list[[i]] <- p

  3. p.list grows to become a list with each numbered element (1:4) holding a single plot, each plot also being a named list.

Stephen Henderson
  • 6,340
  • 3
  • 27
  • 33