使用highcharter自定义boxplot的颜色

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

我在highcharter上有箱图,我想自定义两个

  • 填色
  • 边框颜色

这是我的代码

df = data.frame(cbind(categ = rep(c('a','b','c','d')),value = rnorm(1000)))

hcboxplot(var = df$categ, x = as.numeric(df$value)) %>% 
  hc_chart(type = "column")  %>% 
  hc_colors(c("#203d7d","#a0a0ed","#203d7e","#a0a0ad")) 

hc_colors只有在我放var2而不是var时工作,但随后盒子图缩小了......

r highcharts colors boxplot r-highcharter
2个回答
1
投票

样式fillColor的API:https://api.highcharts.com/highcharts/series.boxplot.fillColor

而对于“边框颜色”:https://api.highcharts.com/highcharts/series.boxplot.color

关于如何设置和定义点的纯JavaScript示例:https://jsfiddle.net/BlackLabel/6tud3fgx

和R代码:

library(highcharter)
df = data.frame(cbind(categ = rep(c('a','b','c','d', 'e')),value = rnorm(1000)))
hcboxplot(var = df$categ, x = as.numeric(df$value)) %>% 
  hc_chart(type = "column", events = list(
    load = JS("function() {
        var chart = this;
        chart.series[0].points[2].update({
          color: 'red'
        })
        chart.series[0].points[4].update({
          x: 4,
          low: 600,
          q1: 700,
          median: 800,
          q3: 900,
          high: 1000,
          color: 'orange'
        })
      }")
  ))  %>% 
  hc_plotOptions(boxplot = list(
    fillColor = '#F0F0E0',
    lineWidth = 2,
    medianColor = '#0C5DA5',
    medianWidth = 3,
    stemColor = '#A63400',
    stemDashStyle = 'dot',
    stemWidth = 1,
    whiskerColor = '#3D9200',
    whiskerLength = '20%',
    whiskerWidth = 3,
    color = 'black'
  ))  %>% 
  hc_colors(c("#203d7d","#a0a0ed","#203d7e","#a0a0ad"))

0
投票

由于还没有highcharter答案,我给你至少一个基本解决方案。

首先,您对数据框架的定义有些缺陷,而是:

dat <- data.frame(categ=c('a','b','c','d'), value=rnorm(1000))

现在,使用boxplot非常简单。 border选项为您的边框着色。使用选项col你也可以填充颜色。

boxplot(value ~ categ, dat, border=c("#203d7d","#a0a0ed","#203d7e","#a0a0ad"), pars=list(outpch=16))

enter image description here

注意:有关进一步的自定义,请参阅this nice solution

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