我有一个Seasonal包的矩阵输出,我过滤掉了“预测”列,只留下时间(月份)和“lowerci”和“upperci”条目。
这通过以下方式完成:
season13201101FL.forecast[,c('lowerci','upperci')]
数据样本:
lowerci upperci
Oct 2017 2415.8826 3083.332
Nov 2017 2217.2670 3238.572
Dec 2017 1976.0041 3181.648
Jan 2018 2048.9771 3577.373
Feb 2018 2046.3051 3834.099
这是“mts”类。
我正在使用highcharter库来绘制我的值。但是,即使我使用series.keys
来映射,它似乎也没有同时使用“lowerci”和“upperci”列。:
hc <- highchart(type = "stock") %>%
hc_add_series(season13201101FL, id = "Original", name = "Original-FL") %>%
hc_add_series(season13201101FL.seasonalData, id = "Seasonally Adjusted-FL", name = "Seasonally Adjusted") %>%
hc_add_series(season13201101FL.forecast[,c('forecast')], id = "Forecast-FL") %>%
hc_add_series(season13201101FL.forecast[,c('lowerci','upperci')], id = "ForecastRange-FL", keys = c('x', 'low', 'high'), type = "arearange")
hc
结果图表显示原始,季节性调整和预测系列,但预测范围显示没有连接点的“线”,每个时间条目只有一个实际数据点。如何让highcharter看到这是一个arearange
系列?
要重现,请将以下内容用作导入CSV theCSV
:
date count
2008.0027 45778
2008.0874 50460
2008.1667 62162
2008.2514 55999
2008.3333 51571
2008.418 45044
2008.5 46357
2008.5847 48498
2008.6694 45472
2008.7514 47161
2008.8361 41907
2008.918 39131
2009.0027 33810
2009.0877 34469
然后代码是:
library(shiny)
library(highcharter)
library(dplyr)
library(tidyr)
library(seasonal)
seasonData <- ts(theCSV[,-1], frequency = 12, start = c(2008,1));
seasonData.seas <- seas(seasonData);
seasonData.seasonalData <- final(seasonData.seas);
seasonData.forecast <- series(seasonData.seas, "forecast.forecasts");
seasonData.seasComp <- series(seasonData.seas, "seats.seasonal");
hc <- highchart(type = "stock") %>%
hc_add_series(seasonData, id = "Original", name = "Original-FL") %>%
hc_add_series(seasonData.seasonalData, id = "Seasonally Adjusted-FL", name = "Seasonally Adjusted") %>%
hc_add_series(seasonData.forecast[,c('forecast')], id = "Forecast-FL") %>%
hc_add_series(seasonData.forecast[,c('lowerci','upperci')], id = "ForecastRange-FL", keys = c('x', 'low', 'high'), type = "arearange")
hc;
答案 0 :(得分:1)
一种方法是使用值和日期/时间值将数据预测转换为数据框。
要获取datetime
值,您可以使用time
和as.Date
函数。然后
使用hc_add_series
添加数据。
library(highcharter)
library(dplyr)
library(tidyr)
library(seasonal)
seasonData <- AirPassengers
seasonData.seas <- seas(seasonData);
seasonData.seasonalData <- final(seasonData.seas);
seasonData.forecast <- series(seasonData.seas, "forecast.forecasts");
seasonData.seasComp <- series(seasonData.seas, "seats.seasonal");
time <- seasonData.forecast %>%
stats::time() %>%
zoo::as.Date() %>%
datetime_to_timestamp()
dfforecast <- seasonData.forecast %>%
as.data.frame() %>%
mutate(time = time)
highchart(type = "stock") %>%
hc_add_series(seasonData, id = "Original", name = "Original-FL") %>%
hc_add_series(seasonData.seasonalData, id = "Seasonally Adjusted-FL", name = "Seasonally Adjusted") %>%
hc_add_series(seasonData.forecast[,c('forecast')], id = "Forecast-FL") %>%
hc_add_series(dfforecast, hcaes(x = time, low = lowerci, high = upperci), id = "ForecastRange-FL", type = "arearange")
hc