2017-02-27 49 views
2

我从教程如下:加入到预测Dygraph

https://rstudio.github.io/dygraphs/

它们显示在底部的预测,我想将它添加到原来的时间序列图。所以,下面是代码,我不知道如何将两者结合起来:

install.packages("dygraphs") 
library(dygraphs) 
library(forecast) 
library(dplyr) 

dygraph(ldeaths) 

enter image description here

然后我做了一个预测:

hw <- HoltWinters(ldeaths) 
predicted <- predict(hw, n.ahead = 72, prediction.interval = TRUE) 

dygraph(predicted, main = "Predicted Lung Deaths (UK)") %>% 
    dyAxis("x", drawGrid = FALSE) %>% 
    dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>% 
    dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1")) 

enter image description here

如何合并两合一图?

回答

2

看起来这个库需要你合并数据,然后在图中添加单独的系列“图层”。有了您的例子中,你可以这样做:

# combine the time series actual and forcasted values 
combined <- cbind(predicted, actual=ldeaths) 

# plot the different values as different series  
dygraph(combined , main = "Predicted Lung Deaths (UK)") %>% 
    dyAxis("x", drawGrid = FALSE) %>% 
    dySeries("actual", label = "actual") %>% 
    dySeries(paste0("predicted.", c("lwr", "fit", "upr")), label = "Predicted") %>% 
    dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1")) 

enter image description here

+0

谢谢你,伟大的工作 –

+0

任何机会,你可以将其更改为$货币也? –

+0

在我的系统上(使用Chrome浏览器的Mac),轴,标题和图例显示,但没有行或区域....全白色背景。我的版本是每个CRAN最新版本。 –