ggseasonplot用于每小时数据,每周季节性等效

时间:2019-05-11 12:33:22

标签: r

我有一个小时分辨率的时间序列,该序列显示每周的季节性。我正在从ggseasonplot包中寻找等效的功能ggplot2,该功能适用​​于每周的季节性。

我看过网上,但没有找到任何类似的功能,除了seasonplotggseasonplot相同,但花哨的更少。

data<-read.csv("series.csv") 
hours <- seq(ISOdatetime(2016,4,18,0,0,0), ISOdatetime(2016,5,22,23,0,0), by=(60*60))
time_series<- xts::xts(data[123118:123957,2],hours)
time_series168 <- ts(data,frequency = 168)

ggseasonplot(time_series168,continuous = TRUE)

R plot

我想在情节上有几周而不是一年。在剧情中,每周的每个小时都是一个季节,我希望“季节”为几天。

我的数据示例:

Datetime            series
2016-04-18 00:00:00 4124
2016-04-18 01:00:00 3932
2016-04-18 02:00:00 3823
2016-04-18 03:00:00 3830
2016-04-18 04:00:00 3797
2016-04-18 05:00:00 3935

1 个答案:

答案 0 :(得分:0)

我想出了一个解决方案,因此我将其发布,以防其他人在寻找它:

plot(1:168,time_series168[1:168],type="l",xaxt = "n",col=rgb(0,0.2,1),xlab="Days of week",ylab="Time series",main="Evolution of time series over the weeks",lwd=1.5)

for(i in 1:4){
   color<-rgb(0.25*i,0.2,1-0.2*i)
   left<-168*i+1
   right<-168*(i+1)
   lines(1:168,time_series168[left:right],type="l",xaxt = "n", col=color, lwd=1.5)
} 

axis(1, at=c(24,48,72,96,120,144,168)-12, labels=c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"))
legend("topright",bty="n",legend=c("into the past","into the future"), col=c(rgb(0,0.2,1), rgb(1,0.2,0.2)), lty=1,lwd=2)

通过设置xaxt = "n"可以取消标记轴,然后可以在labels中用axis标记轴。