Labelling Points on a highcharter scatter chart

痴心易碎 提交于 2020-01-15 12:14:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!