[英]R Highcharter customize legend to show only certain values
I need the legend to show candidate names and their colors where interval == 3. see plot here我需要图例来显示候选名称及其颜色,其中间隔 == 3。见这里的情节
Subsetting the stops df (stops[my.cols$interval==3])) works for the legend, but it also changes the colors on the map.子集 df (stops[my.cols$interval==3])) 适用于图例,但它也会更改地图上的颜色。 I need to keep the colors on each state the same, but I don't want to show a candidate's name more than once in the legend.
我需要保持每个州的颜色相同,但我不想在图例中多次显示候选人的名字。
See MWE below:请参阅下面的 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)
Is there something in hc_plotOptions that can help customize the legend? hc_plotOptions 中是否有可以帮助自定义图例的内容?
This solution grays out the repeated names in the legend, but still shows them: R - highcharter - selective legends at display此解决方案使图例中重复的名称变灰,但仍显示它们: R - highcharter - 显示时的选择性图例
I can set the seed:我可以设置种子:
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)
You subset stops according to the data.frame my.cols?您根据 data.frame my.cols 停止子集? Before it looks like this:
在它看起来像这样之前:
Hope I got your correct, because it's not very clear which legends you want to display:希望我猜对了,因为不太清楚您要显示哪些图例:
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)
These are the packages in my session used to run this:这些是我会话中用来运行它的包:
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.