Highcharter:调整多个系列的颜色

时间:2019-09-07 08:27:49

标签: r r-highcharter

我正在尝试在高彩图上添加多个图层。我不确定如何分别调整每个图层的颜色。我希望每个组在不透明度较低的情况下具有相同的颜色和背景多边形。下面是一个工作示例。也许有更好的方法来构建层。

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

1 个答案:

答案 0 :(得分:0)

这就是您想要的方式

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"))

在这种情况下,我可以使用99

,在末尾再添加两个字符,可以为十六进制颜色添加不透明度

希望这会有所帮助