Search code examples
javascriptrhighchartslegendr-highcharter

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

Subsetting the stops df (stops[my.cols$interval==3])) works for the legend, but it also changes the colors on the map. 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:

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?

This solution grays out the repeated names in the legend, but still shows them: R - highcharter - selective legends at display


Solution

  • 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? Before it looks like this:

    enter image description here

    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) 
    

    enter image description here

    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