javascript - R Highcharter 自定义图例以仅显示某些值

标签 javascript r highcharts legend r-highcharter

我需要图例来显示候选名称及其颜色,其中间隔== 3。 see plot here

对停靠点 df (stops[my.cols$interval==3])) 进行子集化适用于图例,但它也会更改 map 上的颜色。我需要保持每个州的颜色相同,但我不想在图例中多次显示候选人的名字。

参见下面的 MWE:

library(highcharter)
library(usmap)
library(dplyr)

df <- usmap::statepop
df$interval <- sample(c(1,2,3), nrow(df), replace = T)
df$scaled <- sample(1:18, nrow(df), replace = T)

us_small <- download_map_data("countries/us/custom/us-small")


my.cols <- data.frame(
  interval = c(3,2,1),
  scaled = 1:18,
  ContractName = c(rep("Klobuchar",3),rep("Buttigieg",3),rep("Bloomberg",3),rep("Biden",3),rep("Sanders",3),rep("Warren",3)),
  hexes = c(  # GRAY: [
    '#dddddd',
    '#bbbbbb',
    '#888888',
    # PURPLE: [
    '#e8bbdc',
    '#b577a5' ,
    '#7c466e' ,
    # GREEN: [
    '#bbe8ae',
    '#88b57a',
    '#4e7641',
    # BLUE: [
    '#b5cacf',
    '#81b5c0',
    '#578b96' ,
    # RED: [
    '#f9adad',
    '#cf0000',
    '#9f0000',
    # more gray
    "#000000",
    "#696969",
    "#808080"
  ))

stops <- data.frame(
  name = my.cols$ContractName,
  scaled = 1:18,
  from = 0:17/17,
  color = toupper(my.cols$hexes),
  stringsAsFactors = FALSE)

df <- merge(df, stops, by = "scaled")

stops <- list_parse(stops) 




highchart() %>% 
  hc_add_series_map(us_small, df,
                    value = "from", joinBy = c("woe-name", "full"),
                    borderColor = "darkgrey"
                    ,dataLabels = list(enabled = TRUE
                                       ,format = "{point.properties.hc-a2}")) %>% 
  # hc_colorAxis(dataClasses = stops[my.cols$interval==3]) %>%
  hc_colorAxis(dataClasses = stops) %>%
  hc_legend(align = 'right') %>%
  hc_mapNavigation(enabled = FALSE) 

hc_plotOptions 中是否有可以帮助自定义图例的内容?

此解决方案使图例中重复的名称变灰,但仍然显示它们:R - highcharter - selective legends at display

最佳答案

我可以设置种子:

set.seed(111)
df <- usmap::statepop
df$interval <- sample(c(1,2,3), nrow(df), replace = T)
df$scaled <- sample(1:18, nrow(df), replace = T)

您根据 data.frame my.cols 子集停止吗?在它看起来像这样之前:

enter image description here

希望我的理解是正确的,因为还不清楚您要显示哪些图例:

highchart() %>% 
  hc_add_series_map(us_small, df,
                    value = "from", joinBy = c("woe-name", "full"),
                    borderColor = "darkgrey",
                    dataLabels = list(enabled = TRUE
                                       ,format = "{point.properties.hc-a2}")) %>% 
  hc_colorAxis(dataClasses = stops[my.cols$interval==3]) %>%
  hc_legend(align = 'right') %>%
  hc_mapNavigation(enabled = FALSE) 

enter image description here

这些是我的 session 中用于运行此程序的包:

R version 3.6.1 (2019-07-05)

[...]

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.8.3       usmap_0.5.0       highcharter_0.7.0

关于javascript - R Highcharter 自定义图例以仅显示某些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60570525/

相关文章:

javascript - 在 React 应用程序中点击返回按钮不会重新加载页面

r - 尝试使用 "bnlearn"实现一个简单的朴素贝叶斯分类器。不断出现错误 "variables must be either numeric, factors or ordered factors"

javascript - Highcharts 生成不正确

html - "display: none"with highcharts - 大小不正确

javascript - 配置饼图以使用最小空间并且从不隐藏标签

javascript - 仅当值存在时才链接方法 (js)

javascript - Jquery 逻辑按单词匹配数对选择选项进行排序

javascript - HTML - 两个表单,通过 'enter' 按键知道我正在输入哪个表单

在 NumericVector Rcpp 意外行为中返回 NA 值

R:按组和行迭代将数据帧拆分为列表