使用ggplot在多个时间序列上的移动平均值

时间:2019-07-01 00:28:22

标签: r ggplot2 time-series

嗨,我拼命​​尝试绘制12个移动平均值的几个时间序列。

这是一个具有两个时间序列的花和种子密度的示例。 (我还有更多时间序列需要处理...)

#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 

函数ma_12正常工作。当我尝试使用ggplot绘制两个时间序列(花朵和种子)时,问题就来了。我不能将两个分类单元定义为不同的时间序列,也不能对其应用移动平均值。似乎与“ stat_summary”有关...

任何帮助都将受到欢迎!预先感谢

注意:以下链接非常有用,但不能直接帮助我,因为我想应用特定功能并根据一组变量的级别对其进行绘制。目前,我找不到任何解决方案。无论如何,谢谢你给我建议。 Multiple time series in one plot

1 个答案:

答案 0 :(得分:1)

这是您需要的吗?

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