简体   繁体   English

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

[英]Add Annotations to time series with highcharter

I want to add annotations with Highcharter.我想用 Highcharter 添加注释。 I could do it with "classic" scatter plots:我可以用“经典”散点图来做到这一点:

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))
      })
    )
  )

The problem is I would like to do it when my X axis is a Date and somehow I can't make it work问题是当我的 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 likes a timestamp when you use dates like this.当您使用这样的日期时,Highcharter 喜欢时间戳。 Try using the datetime_to_timestamp function as below and include text in your list along with point .尝试使用如下的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