繁体   English   中英

使用 highcharter 向时间序列添加注释

[英]Add Annotations to time series with highcharter

我想用 Highcharter 添加注释。 我可以用“经典”散点图来做到这一点:

library(highcharter)
df <- data.frame(
  x = 1:10,
  y = 1:10
)
hchart(df,"line", hcaes(x = x, y = y))  %>%
  hc_annotations(
    list(
      labels = lapply(1:nrow(df),function(i){
        list(point = list(x = df$x[i],y = df$y[i],xAxis = 0,yAxis = 0))
      })
    )
  )

问题是当我的 X 轴是 Date 时我想这样做,但不知何故我无法让它工作

df <- data.frame(
  x = seq(as.Date("2021-01-01"),as.Date("2021-01-10"),by = "days"),
  y = 1:10
)
hchart(df,"line", hcaes(x = x, y = y))  %>%
  hc_annotations(
    list(
      labels = lapply(1:nrow(df),function(i){
        list(point = list(x = df$x[i],y = df$y[i],xAxis = 0,yAxis = 0))
      })
    )
  )

当您使用这样的日期时,Highcharter 喜欢时间戳。 尝试使用如下的datetime_to_timestamp function 并在列表中包含text以及point

library(highcharter)

df <- data.frame(
  x = seq(as.Date("2021-01-01"),as.Date("2021-01-10"),by = "days"),
  y = 1:10
)

hchart(df, "line", hcaes(x = x, y = y))  %>%
  hc_annotations(
    list(
      labels = lapply(1:nrow(df),function(i){
        list(
          point = list(
            x = datetime_to_timestamp(df$x[i]),
            y = df$y[i],
            xAxis = 0,
            yAxis = 0
            ),
          text = as.character(df$y[i])
          )
      })
    )
  )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM