是否有一种方法可以基于hcboxplot()中的组对盒形图进行着色?

问题描述 投票:0回答:1

我正在尝试使用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))
r highcharts boxplot r-highcharter
1个回答
0
投票

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

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_chart()中的一个简单论点,请告诉我。希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.