I want to add annotations with 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
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. Try using the datetime_to_timestamp
function as below and include text
in your list along with 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])
)
})
)
)