簡體   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