繁体   English   中英

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

[英]R Highcharter customize legend to show only certain values

我需要图例来显示候选名称及其颜色,其中间隔 == 3。见这里的情节

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

请参阅下面的 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 - 显示时的选择性图例

我可以设置种子:

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 停止子集? 在它看起来像这样之前:

在此处输入图片说明

希望我猜对了,因为不太清楚您要显示哪些图例:

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) 

在此处输入图片说明

这些是我会话中用来运行它的包:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM