问题
I want to label each individual point in a highcharter scatter plot. The below code produces a highcharter chart:
library(highcharter)
df = data.frame(name = c("tom", "dick", "harry"),
height = c(170L,180L, 185L),
weight = c(65L, 68L, 75L))
highchart() %>%
hc_add_series(df, type = "scatter", hcaes(x = weight, y = height), showInLegend = FALSE) %>%
hc_tooltip(pointFormat = "height: {point.y} <br> weight: {point.x}")
Hovering over each point shows: "series 1, height: 170, weight: 65" etc. I want the labels to show "tom, height: 170, weight: 65" when hovering over tom and similarly for dick and harry.
Thanks
回答1:
I just added group variable in hcaes
highchart() %>%
hc_add_series(df,type = "scatter",
hcaes(x = weight, y = height, group=name), showInLegend = FALSE) %>%
hc_tooltip(pointFormat = "height: {point.y} <br> weight: {point.x}")
And it worked well.
From the comments, i suggest another options.
Option 1
Change highchart()
to hchart()
hchart(df,type = "scatter",
hcaes(x = weight, y = height, group = name),
showInLegend = FALSE) %>%
hc_tooltip(pointFormat = "height: {point.y} <br> weight: {point.x}")
Option 2
Using marker
option in hc_add_series
.
highchart() %>%
hc_add_series(df,type = "scatter",
hcaes(x = weight, y = height, group = name),
showInLegend = FALSE,
marker = list(symbol = fa_icon("circle")),
color = "blue") %>%
hc_tooltip(pointFormat = "height: {point.y} <br> weight: {point.x}")
来源:https://stackoverflow.com/questions/56122052/labelling-points-on-a-highcharter-scatter-chart