highcharts 使用highcharter为时间序列添加注解

jecbmhm3  于 2022-11-11  发布在  Highcharts
关注(0)|答案(1)|浏览(144)

我想用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轴是日期轴时执行此操作,但不知何故,我无法使其正常工作

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

5vf7fwbs1#

Highcharter喜欢使用时间戳来表示日期。尝试使用datetime_to_timestamp函数,并将textpoint沿着包含在列表中。

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

相关问题