Hi,
My name is Waqas and I am a Ph.D. scholar @ Waikato University. I am currently trying to reproduce a graph from one of my literature to use in my P.h.D defense. I have the time series data of my variable Q that is water discharge I streams and I want to find the local maxima in the series and then fit a spline through them that can show what our variable would look like in the absence of fluctuation. (the light grey line) . Also after finding the spline, how can I find the difference between actual and splined data(dashed line) .
I will be grateful if anyone could help me how can achieve it using ggplot2 in R
The required Result is
and my code is
> library(gglopt2)
>
> data.frame(stringsAsFactors=FALSE,
> Date_Time = c("1/01/2014 1:00", "1/01/2014 2:00", "1/01/2014 3:00",
> "1/01/2014 4:00", "1/01/2014 5:00", "1/01/2014 6:00",
> "1/01/2014 7:00", "1/01/2014 8:00", "1/01/2014 9:00", "1/01/2014 10:00",
> "1/01/2014 11:00", "1/01/2014 12:00", "1/01/2014 13:00",
> "1/01/2014 14:00", "1/01/2014 15:00", "1/01/2014 16:00",
> "1/01/2014 17:00", "1/01/2014 18:00", "1/01/2014 19:00", "1/01/2014 20:00",
> "1/01/2014 21:00", "1/01/2014 22:00", "1/01/2014 23:00",
> "2/01/2014 0:00", "2/01/2014 1:00", "2/01/2014 2:00", "2/01/2014 3:00",
> "2/01/2014 4:00", "2/01/2014 5:00", "2/01/2014 6:00",
> "2/01/2014 7:00", "2/01/2014 8:00", "2/01/2014 9:00", "2/01/2014 10:00",
> "2/01/2014 11:00", "2/01/2014 12:00", "2/01/2014 13:00",
> "2/01/2014 14:00", "2/01/2014 15:00", "2/01/2014 16:00",
> "2/01/2014 17:00", "2/01/2014 18:00", "2/01/2014 19:00", "2/01/2014 20:00",
> "2/01/2014 21:00", "2/01/2014 22:00", "2/01/2014 23:00",
> "3/01/2014 0:00", "3/01/2014 1:00", "3/01/2014 2:00", "3/01/2014 3:00",
> "3/01/2014 4:00", "3/01/2014 5:00", "3/01/2014 6:00",
> "3/01/2014 7:00", "3/01/2014 8:00", "3/01/2014 9:00", "3/01/2014 10:00",
> "3/01/2014 11:00", "3/01/2014 12:00", "3/01/2014 13:00",
> "3/01/2014 14:00", "3/01/2014 15:00", "3/01/2014 16:00",
> "3/01/2014 17:00", "3/01/2014 18:00", "3/01/2014 19:00", "3/01/2014 20:00",
> "3/01/2014 21:00", "3/01/2014 22:00", "3/01/2014 23:00",
> "4/01/2014 0:00", "4/01/2014 1:00", "4/01/2014 2:00", "4/01/2014 3:00",
> "4/01/2014 4:00", "4/01/2014 5:00", "4/01/2014 6:00",
> "4/01/2014 7:00", "4/01/2014 8:00", "4/01/2014 9:00", "4/01/2014 10:00",
> "4/01/2014 11:00", "4/01/2014 12:00", "4/01/2014 13:00",
> "4/01/2014 14:00", "4/01/2014 15:00", "4/01/2014 16:00", "4/01/2014 17:00",
> "4/01/2014 18:00", "4/01/2014 19:00", "4/01/2014 20:00",
> "4/01/2014 21:00", "4/01/2014 22:00", "4/01/2014 23:00",
> "5/01/2014 0:00", "5/01/2014 1:00", "5/01/2014 2:00", "5/01/2014 3:00",
> "5/01/2014 4:00"),
> Q = c(0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.71, 0.71, 0.71,
> 0.71, 0.71, 0.71, 0.45, 0.45, 0.42, 0.42, 0.42, 0.32, 0.32,
> 0.4, 0.4, 0.4, 0.4, 0.42, 0.42, 0.48, 0.51, 0.48, 0.51, 0.48,
> 0.48, 0.48, 0.48, 0.45, 0.45, 0.42, 0.4, 0.37, 0.37, 0.37, 0.37,
> 0.35, 0.35, 0.35, 0.37, 0.37, 0.4, 0.4, 0.42, 0.42, 0.42, 0.42,
> 0.42, 0.42, 0.42, 0.42, 0.42, 0.4, 0.4, 0.37, 0.37, 0.37, 0.35,
> 0.35, 0.35, 0.35, 0.35, 0.35, 0.37, 0.37, 0.37, 0.37, 0.42, 0.48,
> 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.51, 0.51, 0.51, 0.48, 0.45,
> 0.45, 0.42, 0.4, 0.4, 0.4, 0.4, 0.4, 0.42, 0.45, 0.48, 0.48,
> 0.42, 0.48, 0.48)
> )
> p = ggplot(PET, aes(x = Date_Time, group = 1)) + geom_line(aes(y = Q), colour = "blue")+
> stat_peaks(aes(y=Q),colour = "red") +
> geom_line(aes(y = Eto, alpha = 0.5), colour = "orange") + theme_bw()
> p