Highcharts - R 中的 Highcharter - 从 df 而不是 map 获取工具提示值

Highcharts - Highcharter in R - Getting Tooltip values from df rather than map

我正在尝试使用 R 中的 highcharter 创建一个带有突出显示图块的地图。 我设法检索了一张 JSON 地图,用我的数据创建了一个数据框,并生成了一张高图图。我现在正在自定义 toolip,它应该在我的数据框的计数列中显示国家名称、地区和数字整数。使用 hc_tooltip() 函数 returns 名称和区域,但未找到值。我怀疑这是由于工具提示功能查看地图数据而不是我的数据框。我不确定如何改变它。这是一个例子:

map <- "https://code.highcharts.com/mapdata/custom/world-highres.geo.json" %>% 
  GET() %>% 
  content() %>% 
  jsonlite::fromJSON(simplifyVector = FALSE)


sample_table <- data.frame(name = c("Spain","France","China"),
           continent = c("Europe","Europe","Asia"),
           occurences = c(150,70,120))

cols3 <- scales::viridis_pal(option = "turbo",
                             begin = 0.4,
                             direction = 1)(length(unique(sample_table$name)))

highchart(type = "map") %>% 
  hc_title(text = "Map Title") %>%
  hc_subtitle(text = "Map Subtitle") %>%
  hc_add_series_map(map = map, df = sample_table, joinBy = "name", value = "occurences",
                    dataLabels = list(enabled = TRUE
                                      ,format = "{point.properties.hc-a2}")
  ) %>%
  hc_colors(cols3) %>%
  hc_colorAxis(stops = color_stops(colors = cols3)) %>%
  hc_tooltip(
    useHTML = TRUE,                             
    formatter = JS(
      "
      function(){
        outHTML = '<b>' + this.point.name + '</b> <br>' + this.point.continent +
        '</b> <br>' + this.point.occurences
        return(outHTML)
      }

      "
    ))%>%
  hc_mapNavigation(enabled = TRUE)

其中 table 包含以下内容

    name continent occurences
1  Spain    Europe        150
2 France    Europe         70
3  China      Asia        120

从图中可以看出,我按预期获得了国家和大洲名称,但没有获得出现次数。有没有办法从地图数据和数据框中获取工具提示值?

当您包含 hc_add_series_map 时,您将要绘制图表的变量的 value 定义为“发生次数”。要将此列包含在您的 formatter 中,请尝试使用:

this.point.value

而不是引用列名。

完整示例如下:

library(highcharter)
library(httr)

map <- "https://code.highcharts.com/mapdata/custom/world-highres.geo.json" %>% 
  GET() %>% 
  content() 

sample_table <- data.frame(name = c("Spain","France","China"),
                           continent = c("Europe","Europe","Asia"),
                           occurences = c(150,70,120))

cols3 <- scales::viridis_pal(option = "turbo",
                             begin = 0.4,
                             direction = 1)(length(unique(sample_table$name)))

highchart(type = "map") %>% 
  hc_title(text = "Map Title") %>%
  hc_subtitle(text = "Map Subtitle") %>%
  hc_add_series_map(map = map, 
                    df = sample_table, 
                    joinBy = "name", 
                    value = "occurences",
                    dataLabels = list(enabled = TRUE,format = "{point.properties.hc-a2}")
  ) %>%
  hc_colors(cols3) %>%
  hc_colorAxis(stops = color_stops(colors = cols3)) %>%
  hc_tooltip(
    useHTML = TRUE,                             
    formatter = JS(
      "
      function(){
        outHTML = '<b>' + this.point.name + '</b> <br>' + this.point.continent +
        '</b> <br>' + this.point.value;
        return(outHTML)
      }
      "
    )) %>%
  hc_mapNavigation(enabled = TRUE)

地图