Search code examples
rr-highcharter

Highcharter: Adjust colours for multiple series


I am trying to add multiple layers to highcharter plots. I am not sure how to adjust colours for each layer independently. I want each group to have the same colour and the background polygon at lower opacity. Below is a working example. Perhaps there is a better way to build up layers.

library(highcharter)
data(iris)
hull <- data.frame(x=c(5.5,4.5,4.3,4.6,5.2,5.7,5.8,5.7,6.2,5,4.9,5.4,6,7,6.8,7.7,6,4.9,6.2,7.7,7.9),y=c(3.5,2.3,3,3.6,4.1,4.4,4,3.8,2.2,2,2.4,3,3.4,3.2,2.8,2.6,2.2,2.5,3.4,3.8,3.8),Species=c('setosa','setosa','setosa','setosa','setosa','setosa','setosa','setosa','versicolor','versicolor','versicolor','versicolor','versicolor','versicolor','versicolor','virginica','virginica','virginica','virginica','virginica','virginica'))

hchart(hull,"polygon",hcaes(x,y,group="Species",opacity=0.2)) %>%
  hc_add_series(data=iris,type="scatter",hcaes(Sepal.Length,Sepal.Width,group="Species"),showInLegend=F) %>%
  hc_colors(colors=c("#A6CEE3","#1F78B4","#B2DF8A","#33A02C"))

enter image description here


Solution

  • is this someway in the way you want it

    hchart(hull,"polygon",hcaes(x,y,group="Species",opacity=0.5)) %>%
      hc_add_series(
        data=iris,type="scatter",
        hcaes(
          Sepal.Length,
          Sepal.Width,
          group="Species", 
          color = c(setosa = "#A6CEE3",versicolor = "#1F78B4",virginica = "#B2DF8A")[Species]
        ),
        showInLegend=F
      ) %>%
      hc_colors(colors=c("#A6CEE399","#1F78B499","#B2DF8A99","#33A02C99"))
    

    You can add opacity to hex colors by adding two more characters at the end in this case I used 99

    Hope this helps