高位散布图上的标记点

时间:2019-05-14 02:04:40

标签: r r-highcharter

我想标记一个高章程散点图中的每个单独的点。下面的代码生成一个高位图表:

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

将鼠标悬停在每个点上会显示:“系列1,高度:170,重量:65”等。 我希望将标签悬停在tom上时,标签应显示“ tom,身高:170,体重:65”,并且类似地表示迪克和哈利。

谢谢

1 个答案:

答案 0 :(得分:1)

我刚刚在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}")

效果很好。

从评论中,我建议其他选择。

选项1

highchart()更改为hchart()

hchart(df,type = "scatter",
       hcaes(x = weight, y = height, group = name),
       showInLegend = FALSE) %>%
  hc_tooltip(pointFormat = "height: {point.y} <br> weight: {point.x}")

选项2

marker中使用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}")