如何从矩阵绘制 Highcharter 区域范围

How to plot Highcharter arearange from matrix

我有一个来自 Seasonal 包的矩阵输出,我过滤掉了 "forecast" 列,只留下时间(Month Year)以及 "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" class 的。 我正在使用 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

生成的图表显示了原始、季节性调整和预测系列,但显示的预测范围没有 "line" 连接点,每个时间条目只有一个实际数据点。如何让 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;

一种方法是将预测转换为具有值和 dates/time 值的数据框。

要获取 datetime 值,您可以使用 timeas.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