R 使用ggplot对多个时间序列进行移动平均

R 使用ggplot对多个时间序列进行移动平均,r,ggplot2,time-series,R,Ggplot2,Time Series,嗨,我拼命地用12个月的移动平均线绘制几个时间序列 这是一个花和种子密度的两个时间序列的例子。(我有更多的时间序列要处理…) #数据集 分类单元这就是你需要的 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(

嗨,我拼命地用12个月的移动平均线绘制几个时间序列

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

#数据集
分类单元这就是你需要的

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

f谢谢你的链接,很有趣。然而,我的问题完全不同,因为我想应用一个特定的函数,并根据一个组变量的级别绘制它。目前,我找不到任何类似的主题…快速选项:
ggplot(df,aes(ymd,密度,颜色=因子(分类单元))+geom_smooth(fun.y=ma_12,stat=“summary”)
或者只是事先进行计算并正常绘制相同问题:“计算失败于
stat_summary()
:指定的时间序列参数无效”。但是是的,这是一种更快的编码方式,谢谢!是的,这个有效!!多谢各位。我将尝试对许多变量进行推广和扩展。
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))