Tony2016
Tony2016

Reputation: 267

Labelling Points on a highcharter scatter chart

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

Upvotes: 1

Views: 1892

Answers (1)

Steve Lee
Steve Lee

Reputation: 776

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

Upvotes: 3

Related Questions