3
$\begingroup$

My intention is to change the style of the bottom of the BoxWhisker Chart after drawing of the BoxWhiskerChart. For instance: lines of the BoxWhiskerChart are Thick but change to dotted bellow threshold=0.1.

The goal is to show that the part of the data is bellow the detection limit

My simple experiment

 data= RandomReal[{0, 1}, 1000];

 BoxWhiskerChart[data, {{"Whiskers", If[# < 0.1, Dashed, Thick] &}}]

anyway don't work. But my ideal is be able to change the style of the entire part of graph bellow threshold - include whiskers, quantiles, outliers... if they are < threshold.

$\endgroup$

1 Answer 1

5
$\begingroup$

You can use a custom ChartElementFunction as follows:

ClearAll[cF]
cF[t_: 0][cedf_: "BoxWhisker", o : OptionsPattern[]] := 
  ChartElementDataFunction[cedf, o][##] /. 
      l : Line[{l1 : {{x_, b_}, {x_, a_}}, l2 : {{x_, c_}, {x_, d_}}}] :> 
   {CapForm["Butt"], 
    Which[t <= a, l, t >= d, {Dashed, l}, 
      t >= c, {Line[l2 /. c -> t], Dashed, Line[l2 /. d -> t], Line[l1]},
      t <= b, {Line[l1 /. a -> t], Line[l2], Dashed, Line[l1 /. b -> t]},
      True, {Line[l2], Dashing[{Small, Small}], Line[l1]}]} &

Examples:

data = RandomReal[{0, 1}, 1000];
BoxWhiskerChart[{data, 1.2 data}, {{"Whiskers", Thick}}, 
 ChartElementFunction -> cF[.2][]]

enter image description here

 BoxWhiskerChart[{data, 1.2 data}, {{"Whiskers", Thick}},
   ChartElementFunction -> cF[.9]["GlassBoxWhisker"], ChartStyle->"Rainbow"]

enter image description here

Update: Adding dashed edges below the threshold per Okkes's suggestion in the comments:

ClearAll[cF2]
cF2[t_: 0][cedf_: "BoxWhisker", o : OptionsPattern[]] := Module[{qt = Quartiles[#2]},
  {CapForm["Butt"], ChartElementDataFunction[cedf, o][##] /. 
       l : Line[{l1 : {{x_, b_}, {x_, a_}}, l2 : {{x_, c_}, {x_, d_}}}] :> 
   Which[t <= a, l, t >= d, {Dashed, l}, 
         t >= c, {Line[l2 /. c -> t], Dashed, Line[l2 /. d -> t], Line[l1]}, 
         t <= b, {Line[l1 /. a -> t], Line[l2], Dashed, Line[l1 /. b -> t]}, 
         True, {Line[l2], Dashed, Line[l1]}], 
   Which[t >= qt[[-1]], {EdgeForm[{Dashed, Thick, Black}], FaceForm[], 
          Polygon @ {{#[[1, 1]],  qt[[-1]]}, {#[[1, 1]], qt[[1]]},
           {#[[1, 2]], qt[[1]]}, {#[[1, 2]], qt[[-1]]}}}, 
     qt[[-1]] > t > qt[[1]], {Thick, Black, 
          Line @ {{#[[1, 1]], t}, {#[[1, 1]],  qt[[-1]]}, 
           {#[[1, 2]], qt[[-1]]}, {#[[1, 2]], t}}, 
          Dashed, Line @ {{#[[1, 1]], t}, {#[[1, 1]], 
               qt[[1]]}, {#[[1, 2]], qt[[1]]}, {#[[1, 2]], t}}}, 
     True, {}]}] &

Example:

data = RandomReal[{0, 1}, 1000];
t = .85;
BoxWhiskerChart[{data, 1.5 data}, {{"Whiskers", Thick}}, 
 GridLines -> {None, {t}},
  ChartElementFunction -> cF2[t]["GradientScaleBoxWhisker", "ColorScheme" -> "Rainbow"], 
 ImageSize -> Large]

enter image description here

$\endgroup$
4
  • $\begingroup$ This is good but not informative if val<0.5. Is it possible make dashed edge form for the box when val<0.5? $\endgroup$ Commented May 3, 2019 at 15:19
  • $\begingroup$ @OkkesDulgerci, good point. It might be easier to use Gridlines->{None, {val}}. I will update with a version that adds dashed edges. $\endgroup$
    – kglr
    Commented May 3, 2019 at 19:39
  • $\begingroup$ @Okkes, please see the update. $\endgroup$
    – kglr
    Commented May 3, 2019 at 20:41
  • $\begingroup$ Looks great! Two more suggestion. Top of left box should be also dashed. Edge of right box should be solid when t>0.85 (including top)? Even fence could be dashed? $\endgroup$ Commented May 3, 2019 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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