R中的Highchart工具提示

yeotifhr  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(141)

我有两个数据集(initial_tumors和modified_recurring_tumors)。
输入:

initial_tumors:

structure(
  list(
    number = c(1L, 3L, 2L, 4L, 5L, 6L, 8L),
    count = c(145L,
              47L, 38L, 25L, 16L, 14L, 9L),
    tumor_category = c(
      "1 Tumor",
      "3 Tumors",
      "2 Tumors",
      "4 Tumors",
      "5 Tumors",
      "6 Tumors",
      "8 or more Tumors"
    )
  ),
  row.names = c(NA,-7L),
  class = c("tbl_df", "tbl", "data.frame")
)

data2:
modified_recurring_tumors:

structure(
  list(
    recur = c(NA, 5L, 1L),
    count = c(111L, 46L, 45L),
    tumor_category = c("8 or more Tumors", "5 Tumors", "1 Tumor")
  ),
  row.names = c(NA,-3L),
  class = c("tbl_df", "tbl", "data.frame")
)

我正在使用highchart绘制初始肿瘤与复发肿瘤的图表。我正在使用工具提示,以便鼠标悬停时显示tumor_category详细信息,例如:
初始肿瘤:4肿瘤计数:16
复发性肿瘤:4肿瘤计数:18
我使用以下highchart代码:

highchart() %>%
  hc_chart(type = "bubble") %>%
  hc_add_series(
    data = initial_tumors$count,
    name = "Initial tumors",
    type = "bubble",
    color = "purple",
    dataLabels = list(enabled = TRUE, format = "{point.y}")
  ) %>%
  hc_add_series(
    data = modified_recurring_tumors$count,
    name = "Recurring tumors",
    type = "spline",
    color = "red",
    dataLabels = list(enabled = TRUE, format = "{point.y}")
  ) %>%
  hc_tooltip(
    shared = FALSE,
    formatter = JS(
      "function() {
      var tumorCategory;
      if (this.series.name === 'Initial tumors' || this.series.name === 'Initial tumor categories') {
        tumorCategory = this.point.y;
      } else if (this.series.name === 'Recurring tumors' || this.series.name === 'Recurring tumor categories') {
        tumorCategory = this.point.y;
      }
      var tooltip = '<b>' + this.series.name + '</b><br>   : ' + tumorCategory + '<br>Count: ' + this.y;
      return tooltip;
    }"
    )
  )  %>%
  hc_title(text = "Initial vs Recurring Tumors")

但是当我将鼠标悬停在图形上时,它没有显示tumor_category,而是显示count。
如何纠正?

axzmvihb

axzmvihb1#

当你使用hc_add_series时,你只传入了列值;如果你改为传入data.frame,你就可以访问其他列的数据。使用hcaes可以定义y轴值(计数)。在格式化程序中,可以使用this.point.tumor_category访问类别,使用this.point.count访问计数。

library(tidyverse)
library(highcharter)
  
highchart() %>%
  hc_chart(type = "bubble") %>%
  hc_add_series(
    data = initial_tumors,
    hcaes(y = count),
    name = "Initial tumors",
    type = "bubble",
    color = "purple",
    dataLabels = list(enabled = TRUE, format = "{point.y}")
  ) %>%
  hc_add_series(
    data = modified_recurring_tumors,
    hcaes(y = count),
    name = "Recurring tumors",
    type = "spline",
    color = "red",
    dataLabels = list(enabled = TRUE, format = "{point.y}")
  ) %>%
  hc_tooltip(
    shared = FALSE,
    formatter = JS(
      "function() {
        var tooltip = '<b>' + this.series.name + '</b>' + 
                      '<br>Tumor category: ' + this.point.tumor_category + 
                      '<br>Count: ' + this.point.count;
        return tooltip;
      }"
    )
  ) %>%
  hc_title(text = "Initial vs Recurring Tumors")

相关问题