1

我每天都在处理按日期分组的数字范围数据,就像股票数据一样。使用 R Highcharter 股票图时,该图正确地获取了数据点/线的数值,但日期均列为 1970 年 1 月 1 日。

以前提出过这个问题:R HighCharter Stock Graph not correct pull dates but the method for working with the dates and successful uses the hc_add_series_time_values which is now deprecated in the Highcharter package。

给定 R 中的数据集:

date <- as.Date(c('2020-01-01','2020-01-02','2020-01-03', '2020-01-04', '2020-01-05'))
source <- c(21, 27, 38, 22, 33)
test <- data.frame(date, source)

我期望以下内容:

highchart(type = "stock") %>%
hc_add_series(test$source, type = "line")

生成带有 x 轴日期的股票图表。

我还查看了带有时间序列数据的 Highcharter plotBands、plotLines,认为日期列需要特殊的转换,但也没有产生预期的结果。

现在不推荐使用 highcharter stock 组件的时间值函数以使 x 轴接受日期时间值作为日期时间值,那么正确的过程是什么?

4

1 回答 1

1

也许您可以将数据框更改为时间序列并仍然使用hc_add_series.

library(tidyverse)
library(highcharter)
library(xts)

test_xts <- xts(test[-1], order.by = date)

highchart(type = "stock") %>%
  hc_add_series(test_xts, type = "line") %>%
  hc_xAxis(labels = list(format = '{value:%b %d}'))

输出

在 xaxis 上带有日期的 highcarter 图

数据

date <- as.Date(c('2020-01-01','2020-01-02','2020-01-03', '2020-01-04', '2020-01-05'))
source <- c(21, 27, 38, 22, 33)
test <- data.frame(date, source)
于 2020-02-13T17:18:38.370 回答