First problem - in the zoo()
function time
and A
should be placed in opposite order.
temp.zoo<-zoo(df$A,df$time)
temp2.zoo<-zoo(df$B,df$time)
Next, for rollmean()
function you should add argument fill=list(NA,NULL,NA)
to fill start and end with NA values to get the same length vector as original data.
m.av<-rollmean(temp.zoo,10,fill=list(NA,NULL,NA))
df$A.av=coredata(m.av)
m2.av<-rollmean(temp2.zoo,10,fill=list(NA,NULL,NA))
df$B.av=coredata(m2.av)
Now you can melt the original data frame before plotting. So for plotting you will need only one geom_line()
call.
df.long<-melt(df, id.vars="time")
ggplot(data=df.long, aes(time,value,color=variable))+geom_line()