R-highcharter 组合工具提示

时间:2021-06-24 08:55:36

标签: r highcharts r-highcharter

我正在使用 highcharter,但我遇到了以下问题。在最后的 https://jkunst.com/highcharter/articles/stock.html 中,您有堆叠图表,并且每个系列的工具提示都同时显示:

enter image description here

假设我有一个由

创建的图表
require(highcharter)

pd1 <- data.frame(x=1:10,y=rnorm(10))

plt <- highchart() %>% 
  hc_add_series(pd1, "scatter", hcaes(x = x, y = y), color = "#7cb4ed",
                tooltip = list(headerFormat="<b> data1 <b> <br/>", 
                               pointFormat = "x1: {point.x} <br/> y1: {point.y}"), 
                name = "data1")

pd2 <- data.frame(x=1:10,y=rnorm(10))

plt <- plt %>% 
  hc_add_series(pd2, "line", hcaes(x = x, y = y), color = "red",
                tooltip = list(headerFormat="<b> data2 <b> <br/>", 
                               pointFormat = "x2: {point.x} <br/> y2: {point.y}"), 
                name = "data2")

plt

我怎样才能实现与图片相同的效果,即在一个点上移动时会显示所有工具提示? 我查看了 hc_tooltip 函数并找到了 shared,但 plt %>% hc_tooltip(shared=TRUE) 不起作用。

1 个答案:

答案 0 :(得分:1)

问题是共享工具提示根本不应该在散布系列上工作,因为它们没有按 X 递增顺序进行排序和布局:https://github.com/highcharts/highcharts/issues/1431

您可以在此处找到建议的解决方法: Scatter tooltip of highchart is not being displaying

library('highcharter')
highchart() %>%
  hc_tooltip(shared=TRUE) %>%
  hc_add_series(
    type = "line",
    data = list(5, 4, 3, 5)
  ) %>%
  hc_add_series(
    type = "line",
    lineWidth = 0,
    data = list(15, 14, 13, 15)
  )