3
$\begingroup$

With BoxWhiskerChart you can specify a gradient scale fill for the boxes using the ChartElementFunctionand the "GradientScaleBoxWhisker" ChartElementDataFunction.

SeedRandom[456];
data = RandomVariate[LogNormalDistribution[#, .04], 20] & /@ {.1, .16};

BoxWhiskerChart[data, "Outliers", 
 ChartElementFunction -> ChartElementDataFunction["GradientScaleBoxWhisker", 
   "ColorScheme" -> "RoseColors"]]

Mathematica graphics

The gradient fills over the range of the chart. I would like it to fill over the first to third quartile range of each data series; Red at first quartile, Orange at second quartile, and Green at third quartile.

sc = Function[{x}, Blend[{{#1, Red}, {#2, Orange}, {#3, Green}}, x]] &[
    Sequence @@ #] & /@ (Quantile[#, {0.25, .5, 0.75}] & /@ data)
(* 
{Function[{x}, Blend[{{1.04715, Red}, {1.07836, Orange}, {1.14039, Green}}, x]], 
 Function[{x}, Blend[{{1.16097, Red}, {1.18109, Orange}, {1.19066, Green}}, x]]}
*)

Using sc in place of "RoseColors" errors out.

BoxWhiskerChart[data, "Outliers", 
 ChartElementFunction -> ChartElementDataFunction["GradientScaleBoxWhisker", 
   "ColorScheme" -> sc]]

I think I would also need some form of ColorFunctionScaling -> False as well. However, this option is not valid for either BoxWhiskerChart nor ChartElementDataFunction.

I would also like to color the top whisker and fence Green and the bottom Red. The element styling only appears to allow one style for both.

Is there a way to get the gradient fill and whisker/fence styling I want by using BoxWhiskerChart? I would prefer to use BoxWhiskerChart instead of drawing it all with Graphics so I have the options of all the element and outlier features of this function.

$\endgroup$
  • $\begingroup$ Maybe you need to compact your title:) $\endgroup$ – xyz Jul 11 '16 at 3:31
2
$\begingroup$

Using your sc to define a list of colors as the value for the "ColorScheme" option in a custom ChartElementFunction:

ClearAll[ceF];
ceF[cu_: Green, cl_: Red] := Module[{cs = Module[{qt = Quartiles@#2}, 
  (Function[{x}, Blend[{{#1, Red}, {#2, Orange}, {#3, Green}}, x]] & @@ qt) /@ 
    Subdivide[qt[[1]], qt[[-1]], 20]]}, 
   ChartElementDataFunction["GradientScaleBoxWhisker", "ColorScheme" -> cs][##] /. 
    Line[{x : {__List}, y : {__List}}] :> {cl, Line[x], cu, Line[y]}] &

BoxWhiskerChart[data, "Outliers", ChartElementFunction -> ceF[]]

enter image description here

BoxWhiskerChart[data, "Outliers", ChartElementFunction ->
 ceF[Directive[Thick, Dashed, Purple], Directive[Thick, Brown]] ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.