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