0

Hi I try desperately to plot several time series with a 12 months moving average.

Here is an example with two time series of flower and seeds densities. (I have much more time series to work on...)

#datasets
taxon <- c(rep("Flower",36),rep("Seeds",36))
density <- c(seq(20, 228, length=36),seq(33, 259, length=36))
year <- rep(c(rep("2000",12),rep("2001",12),rep("2002",12)),2)
ymd <- c(rep(seq(ymd('2000-01-01'),ymd('2002-12-01'), by = 'months'),2))

#dataframe
df <- data.frame(taxon, density, year, ymd)

library(forecast)

#create function that does a Symmetric Weighted Moving Average (2x12) of the monthly log density of flowers and seeds 
ma_12 <- function(x) {
  ts_x <- ts(x, freq = 12, start = c(2000, 1), end = c(2002, 12)) # transform to time-series object as it is necessary to run the ma function
  return(ma(log(ts_x + 1), order = 12, centre = T))
}


#trial of the function
ma_12(df[df$taxon=="Flower",]$density)  #works well

library(ggplot2)

#Trying to plot flower and seeds log density as two time series 
ggplot(df,aes(x=year,y=density,colour=factor(taxon),group=factor(taxon))) +
  stat_summary(fun.y = ma_12, geom = "line")  #or geom = "smooth"

#Warning message:
#Computation failed in `stat_summary()`:
#invalid time series parameters specified 

Function ma_12 works correctly. The problem comes when I try to plot both time-series (Flower and Seed) using ggplot. I cannot define both taxa as different time series and apply a moving average on them. Seems that it has to do with "stat_summary"...

Any help would be more than welcome! Thanks in advance

Note: The following link is quite useful but can not directly help me as I want to apply a specific function and plot it in accordance to the levels of one group variable. For now, I can't find any solution. Any way, thank you to suggest me this. Multiple time series in one plot

J.marine
  • 69
  • 1
  • 8
  • 1
    Thanks for the link, it is interesting. However, my question is quite different as I want to apply a specific function and plot it in accordance to the levels of one group variable. For now, I can't find any similar topics... – J.marine Jul 01 '19 at 00:50
  • quick option: `ggplot(df, aes(ymd, density, color = factor(taxon))) + geom_smooth(fun.y = ma_12, stat = "summary")` Or just do the calculations beforehand and plot normally – alistaire Jul 01 '19 at 01:50
  • Same issue: "Computation failed in `stat_summary()`:invalid time series parameters specified". But yes, it is a faster way to code, thanks! – J.marine Jul 01 '19 at 02:00

1 Answers1

1

This is what you need?

f <- ma_12(df[df$taxon=="Flower", ]$density)
s <- ma_12(df[df$taxon=="Seeds", ]$density)

f <- cbind(f,time(f))
s <- cbind(s,time(s))

serie <- data.frame(rbind(f,s),
                taxon=c(rep("Flower", dim(f)[1]), rep("Seeds", dim(s)[1])))
serie$density <- exp(serie$f)

library(lubridate)
serie$time <- ymd(format(date_decimal(serie$time), "%Y-%m-%d"))

library(ggplot2)
ggplot() + geom_point(data=df, aes(x=ymd, y=density, color=taxon, group=taxon)) +
geom_line(data=serie, aes(x= time, y=density, color=taxon, group=taxon))