Highcharter plotBands,plotLines with time series data

时间:2017-01-10 16:29:28

标签: r highcharts

在绘制时间序列时,在highcharter库的plotLinesplotBands中指定值的正确方法是什么?使用以下代码,绘图线显示在图表的左端,并且乐队根本不显示。如果我没有指定type = 'stock',即使没有出现情节线。这似乎是时间序列数据的问题,只有与其他类型的数据一样工作正常。所以我相信我可能没有以正确的格式指定值。除了代码中的那个,我还尝试了时间序列格式,例如from = c(1990,1)但它也没有用。

library(highcharter)    
data =ts(data = sample(c(50:100),360, replace = TRUE), start = c(1987,1), frequency = 12, names = 'index')

highchart(type = 'stock')%>%
      hc_add_series_ts(data) %>%
      hc_xAxis(type = 'datetime',
               plotLines = list(
                 list(
                   label = list(text = "This is a plotLine"),
                   color = "#FF0000",
                   width = 5,
                   value = as.Date('1990-01-01', tz = 'UTC')
                   )
                 ),
                 plotBands = list(
                   list(
                     label = list(text = "This is a plotBand"),
                     color = "rgba(100, 0, 0, 0.1)",
                     from = as.Date('1995-01-01', tz = 'UTC'),
                     to = as.Date('1996-01-01', tz = 'UTC')
                     )
                   )
               )

这是结果图表 enter image description here

1 个答案:

答案 0 :(得分:3)

您需要使用datetime_to_timestamp函数转换所有日期值。

这是来自:

 from = as.Date('1995-01-01', tz = 'UTC'),
 to = as.Date('1996-01-01', tz = 'UTC')

要:

 from = datetime_to_timestamp(as.Date('1995-01-01', tz = 'UTC')),
 to = datetime_to_timestamp(as.Date('1996-01-01', tz = 'UTC'))

详细说明:

suppressPackageStartupMessages(library(highcharter))
dt <- as.Date("1995-01-01", tz = "UTC")
dt
#> [1] "1995-01-01"
datetime_to_timestamp(dt)
#> [1] 788918400000

希望这有帮助。

output