使用 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 轴是日期时我想这样做,但不知何故我无法让它工作
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
函数,并将 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])
)
})
)
)
我想用 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))
})
)
)
当您使用这样的日期时,Highcharter 喜欢使用时间戳。尝试使用下面的 datetime_to_timestamp
函数,并将 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])
)
})
)
)