将移动平均图添加到R中的时间序列图中 [英] Add Moving average plot to time series plot in R

查看:229
本文介绍了将移动平均图添加到R中的时间序列图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot2包中的时间序列图,我已经执行了移动平均线,并且我想将移动平均线的结果添加到时间序列图中。



数据集示例(p31):

ambtemp dt

-1.14 2007-09-29 00:01:57 >
-1.12 2007-09-29 00:03:57

-1.33 2007-09-29 00:05:57

-1.44 2007-09-29 00:07:57

-1.54 2007-09-29 00:09:57

-1.29 2007-09-29 00:11:57



时间序列演示文稿的应用代码:

pre $ 需要(ggplot2)
库)
p29 $ dt = strptime(p31 $ dt,%y-%m-%d%H:%M:%S)
ggplot(p29,aes(dt,ambtemp))+ geom_line()+
scale_x_datetime(breaks = date_breaks(2 hour),labels = date_format(%H:%M))+ xlab(Time 00.00〜24:00(2007-09-29) )+ ylab(Tempreture)+
opts(title =(Node 29))

时间序列演示样本


移动平均线图样本

预期结果样本



挑战在于时间序列数据ov =从数据集中获得,其中包括时间戳和温度,但移动平均数据仅包括平均列,而不包括时间戳,并且拟合这两者会导致不一致。



$ b

解决方案

一个解决方案是从库 zoo <使用 rollmean()函数来计算移动平均线。



在你的问题(p31和p29)中有一些数据框名称的混淆,所以我将使用p 29.

  p29 $ dt = strptime(p29 $ dt,%Y-%m-%d%H:%M:%S)

库(动物园)
#制作数据的动物园对象
temp.zoo< -zoo(p29 $ ambtemp,p29 $ dt)

#使用窗口3计算移动平均值并使第一个和最后一个值为NA(以确保向量的长度相同)
m.av <-rollmean(temp.zoo,3,fill = list(NA,NULL,NA))

#将计算出的移动平均线添加到现有的数据框中
p29 $ amb.av = coredata(m.av)

#移动平均线以红色添加
ggplot(p29, aes(dt,ambtemp))+ geom_line()+
geom_line(aes(dt,amb.av),color =red)+
scale_x_datetime(breaks = date_breaks(5 min), labels = date_format(%H:%M))+
xlab(Time 00.00〜24:00(2007-09-29))+ ylab(Tempreture)+
ggtitle 节点29)

如果线条颜色出现在图例中,则 a必须修改 ggplot() geom_line()中的es() code> scale_colour_manual()应该被添加。

  ggplot(p29,aes(dt ))+ geom_line(aes(y = ambtemp,color =real))+ 
geom_line(aes(y = amb.av,color =moving))+
scale_x_datetime(breaks = date_breaks (5分钟),标签= date_format(%H:%M))+
xlab(时间00.00〜24:00(2007-09-29))+ ylab(Tempreture) +
scale_colour_manual(Lines,values = c(real=black,moving=red))+
ggtitle(Node 29)


I have a plot of time series in ggplot2 package and I have performed the Moving average and I would like to add the result of moving average to the plot of time series.

Sample of Data-set (p31):

ambtemp dt
-1.14 2007-09-29 00:01:57
-1.12 2007-09-29 00:03:57
-1.33 2007-09-29 00:05:57
-1.44 2007-09-29 00:07:57
-1.54 2007-09-29 00:09:57
-1.29 2007-09-29 00:11:57

Applied code for time series presentation:

  Require(ggplot2)
  library(scales)
  p29$dt=strptime(p31$dt, "%Y-%m-%d %H:%M:%S")
  ggplot(p29, aes(dt, ambtemp)) + geom_line() +
     scale_x_datetime(breaks = date_breaks("2 hour"),labels=date_format("%H:%M")) + xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Tempreture")+
     opts(title = ("Node 29"))

Sample of time series presentation

Sample of Moving average plot Sample of expected results

The challenge is that time series data ov=btained from data-set which includes timestamps and temperature but Moving average data include just the average column and not the timestamps and fitting these two can cause inconsistency.

解决方案

One solution is to use rollmean() function from library zoo to calculate moving average.

There is some confusion with data frame names in your question (p31 and p29), so I will use p 29.

p29$dt=strptime(p29$dt, "%Y-%m-%d %H:%M:%S")

library(zoo)
#Make zoo object of data
temp.zoo<-zoo(p29$ambtemp,p29$dt)

#Calculate moving average with window 3 and make first and last value as NA (to ensure identical length of vectors)
m.av<-rollmean(temp.zoo, 3,fill = list(NA, NULL, NA))

#Add calculated moving averages to existing data frame
p29$amb.av=coredata(m.av)

#Add additional line for moving average in red
ggplot(p29, aes(dt, ambtemp)) + geom_line() + 
  geom_line(aes(dt,amb.av),color="red") + 
  scale_x_datetime(breaks = date_breaks("5 min"),labels=date_format("%H:%M")) +
  xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Tempreture")+
  ggtitle("Node 29")

If line colors should appear in legend, then aes() in ggplot() and geom_line() has to be modified and scale_colour_manual() should be added.

  ggplot(p29, aes(dt)) + geom_line(aes(y=ambtemp,colour="real")) +
   geom_line(aes(y=amb.av,colour="moving"))+
   scale_x_datetime(breaks = date_breaks("5 min"),labels=date_format("%H:%M")) + 
   xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Tempreture")+
   scale_colour_manual("Lines", values=c("real"="black", "moving"="red")) +    
   ggtitle("Node 29")

这篇关于将移动平均图添加到R中的时间序列图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆