0

I am trying to loop for every field to create a plot output and the code works just fine for this code. But I am trying to put the total 21 plots into a pdf with each page having 4 plots on it. Unfortunately i am unable to share the data due to confidentiality, but i will very much appreciate if there is any tweak you can advice i make

library(ggplot2)
library(dplyr)

field = unique(toa$ref)
#fields values=284 306 288 292 302 296 273 280 253 191 209 214 185 205 166 165 135  91 109  57  72

for (i in field){
  fn = toa[which(toa$ref==i),]
  fn$day_dif = as.integer(difftime(fn$datetime_string_au,
                                   fn$datetime_string_au[1],units = 'day'))
  vert = data.frame(t(fn[1:1,7:13]), stringsAsFactors = FALSE)
  vert = na.omit(vert)
  vert$diff = as.integer(difftime(as.POSIXct(vert[,1],format="%Y-%m-%d"),
                                 fn$datetime_string_au[1],units = 'day'))
  
  plott = ggplot(fn, aes(day_dif, ndvi)) + ggtitle(paste("Field",i)) +
    geom_point()+
    stat_smooth(method = "gam", formula = y ~s(x), size = 1, se = T, colour = "blue")+
    geom_vline(data = vert, aes(xintercept = vert$diff),linetype = 'dashed') +
    geom_vline(xintercept = 0,lwd=1)+
    geom_hline(yintercept = 0,lwd=1)+
    geom_text(data = vert, aes(x = vert$diff, y = -0.3, label = row.names(vert)),
              angle=90,col=vert$diff)
  print(plott)
}
  • 1
    Please share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If you can't share your data, try to create some example data or use some datasets included in R (iris, mtcars or something like that). Regarding your question: What exactly is your problem? Don't you know how to create a PDF or do you have problems creating four plots on one page? – Martin Gal Jul 17 '21 at 16:10

0 Answers0