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)
}