我正在尝试使用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))
答案 0 :(得分:1)
通过设置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))
答案 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()
中的一个简单论点,请告诉我。
希望这会有所帮助。