score:2

Accepted answer

please see brief mention of similar problem as highcharter github issue here.

i am using the covid19 package to have some readily available data to use as illustration. i hypothetically set a few countries to different cfr_level values as examples (this is just to demonstrate, they do not reflect actual case fatality rates).

with hchart use coloredline and multicolor_series module, which allows for individual segmentcolor values to be used (more info and examples here on plugins and modules). then, you can use a column for group and a different column for segmentcolor.

in addition, i included hc_add_series with empty data to add a custom legend.

edit (12/24/20): if you set color in hcaes in hchart you can get the tooltip hover color to match based on cfr_level. for this example, i put the colors in hex equivalents (cfr_level in this case is a color and not a character value).

library(tidyverse)
library(highcharter)
library(covid19)

df <- covid19()    
            
df$cfr_level <- "#008000"
df$cfr_level[df$id == "ind"] <- "#ffff00"
df$cfr_level[df$id == "usa"] <- "#ff0000"

df %>%
  filter(id %in% c("usa", "bra", "rus", "ind")) %>%
  hchart(type = "coloredline", 
         hcaes(x = date, y = confirmed, group = id, color = cfr_level, segmentcolor = cfr_level),
         showinlegend = f) %>%
  hc_add_series(name = "low", color = "#008000", marker = list(symbol = "line")) %>% 
  hc_add_series(name = "med", color = "#ffff00", marker = list(symbol = "line")) %>% 
  hc_add_series(name = "high", color = "#ff0000", marker = list(symbol = "line")) %>% 
  hc_add_dependency("plugins/multicolor_series.js")

plot

highcharter plot


Related Query

More Query from same tag