有没有办法根据 hcboxplot() 中的组为箱线图着色?

Is there a way to color boxplots based on group in hcboxplot()?

我正在尝试使用 R 中的 highcharter 制作箱线图,所有箱线图都根据其组具有不同的颜色。我不知道如何让所有的盒子都有不同的颜色。我应该怎么做才能解决这个问题?

library(highcharter)
library(viridisLite)
library(dplyr)
hcboxplot(x = mpg$cty, var = mpg$manufacturer, outliers = FALSE) %>%
  hc_chart(type = "column") %>%
  hc_colors(viridis(15))

因为,至少对我来说,评论中的 link 并没有直接引导我找到解决方案,我想在下面提供我的方法:

library(highcharter)
library(viridisLite)
library(magrittr)
library(ggplot2) # just for the mpg data

hcboxplot(x = mpg$cty, var = mpg$manufacturer, outliers = FALSE) %>% 
  hc_plotOptions(boxplot = list(
    # it's only possible to provide one single color. This, we want to replace in JS.
    fillColor = "red")) %>% 
  hc_chart(type = "column", 
           events = list(
             load = JS('function() {
              var chart = this;

              // dput(viridis(15)) and make it available as a list in JS
              var colors = ["#440154FF", "#481B6DFF", "#46337EFF", "#3F4889FF", "#365C8DFF", 
                       "#2E6E8EFF", "#277F8EFF", "#21908CFF", "#1FA187FF", "#2DB27DFF", 
                       "#4AC16DFF", "#71CF57FF", "#9FDA3AFF", "#CFE11CFF", "#FDE725FF"];

              // iterate over all data elements (boxplots) and change the desired property
              for (var i=0; i< chart.series[0].data.length; i++){
              chart.series[0].data[i].box.element.attributes.fill.value = colors[i]
              }
              }')
           )) 

通过设置 hc_plotOptions(series=list(colorByPoint=TRUE)):

library(viridisLite)
library(dplyr)
library(ggplot2) 

hcboxplot(x = mpg$cty, var = mpg$manufacturer, outliers = FALSE) %>%
  hc_chart(type = "column") %>%
  hc_colors(viridis(15)) %>% 
  hc_plotOptions(series=list(colorByPoint=TRUE))