R Highcharter自定义图例以仅显示某些值
R Highcharter customize legend to show only certain values
我需要图例来显示候选名称及其颜色,其中间隔 == 3。
see plot here
子集停靠点 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 中是否有可以帮助自定义图例的内容?
此解决方案将图例中重复的名称变灰,但仍会显示它们:
我可以设置种子:
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.framemy.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
我需要图例来显示候选名称及其颜色,其中间隔 == 3。 see plot here
子集停靠点 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 中是否有可以帮助自定义图例的内容?
此解决方案将图例中重复的名称变灰,但仍会显示它们:
我可以设置种子:
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.framemy.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