簡體   English   中英

帶有x軸的繪圖包含日期

[英]Plot with x-axis contains the day

我用這個數據加載一個數據框(命名庫存):

day               value
2000-12-01 00:00:00 11.809242 
2000-12-01 06:00:00 10.919792 
2000-12-01 12:00:00 13.265208 
2000-12-01 18:00:00 13.005139 
2000-12-02 00:00:00 10.592222  
2000-12-02 06:00:00 8.873160 
2000-12-02 12:00:00 12.292847 
2000-12-02 18:00:00 12.609722 
2000-12-03 00:00:00 11.378299 
2000-12-03 06:00:00 10.510972  
2000-12-03 12:00:00 8.297222  
2000-12-03 18:00:00 8.110486  
2000-12-04 00:00:00 8.066154

我嘗試使用arima()模型實現預測:

requires(forecast)
fit <- Arima(stock$value,c(3,1,2))
fcast <- forecast(fit, h = 5)

之后我想從預測過程中獲取情節。 但是我試着讓x軸有日子。 到現在為止我有這個:

x = as.POSIXct( stock$day , format = "%Y-%m-%d %H:%M:%S" )

plot(fcast, xaxt="n")
a = seq(x[1], by="min", length=nrow(stock))
axis(1, at = a, labels = format(a, "%Y-%m-%d %H:%M:%S"), cex.axis=0.6)

因為,你操縱時間序列對象,最好使用ts-packages,如zooxts ,似乎forecast包被設計用於處理這些對象。

## read the zoo object
## I add a virtual colname aaa here 
## for some reason index=0:1 don't work
library(zoo)
stock <- read.zoo(text='aaa day value
2000-12-01 00:00:00 11.809242 
2000-12-01 06:00:00 10.919792 
2000-12-01 12:00:00 13.265208 
2000-12-01 18:00:00 13.005139 
2000-12-02 00:00:00 10.592222  
2000-12-02 06:00:00 8.873160 
2000-12-02 12:00:00 12.292847 
2000-12-02 18:00:00 12.609722 
2000-12-03 00:00:00 11.378299 
2000-12-03 06:00:00 10.510972  
2000-12-03 12:00:00 8.297222  
2000-12-03 18:00:00 8.110486  
2000-12-04 00:00:00 8.066154',header=TRUE,
                tz='',
                index=1:2)

##  arima works well with zoo objects
fit <- Arima(stock,c(3,1,2))
fcast <- forecast(fit, h = 20)
plot(fcast, xaxt="n")

要繪制日期軸,您應該使用axis.POSIXctaxis.Date 日期對象的axis相當於。 但您應該首先創建日期索引。 在這里,我匯總了原始日期和forecast函數生成的forecast日期。

  a <- c(as.POSIXct(index(stock)),
         as.POSIXct(index(fcast$mean),origin='1970-01-01 00:00.00 UTC'))

然后我用這樣的東西繪制我的軸:

## Note the use of las to rotate the axis 
## you can play with format here
axis.POSIXct(1,at=a,format="%a %H",las=2,cex=0.5)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM