3
$\begingroup$

I have the following for a dataset, but I do not have the underlying data:

  • Mean
  • Median
  • Q1
  • Q3
  • range OR max(data) & min(data)

Is it possible for me to draw the BoxWhiskerChart?

$\endgroup$

2 Answers 2

8
$\begingroup$

You can use the option Method with suboption "BoxRange":

ClearAll[boxRange]
boxRange = #[[{5, 3, 2, 4, 6}]] &;

Examples:

summary = {mean, median, q1, q2, min, max} = {5, 6, 3, 7, 1, 9};
summaries = {summary , 2 + summary , 1 + summary};
BoxWhiskerChart[summaries, 
 ChartStyle -> 97, 
 Method -> {"BoxRange" -> boxRange}, 
 ChartElementFunction -> "GlassBoxWhisker"]

enter image description here

If you need to add the mean marker, you can pre-process input data to create new lists with the required means:

ClearAll[preProcess]
preProcess = Module[{d = Append[#, t]}, d /. Solve[Mean[d] == d[[1]], t][[1]]] &;

BoxWhiskerChart[preProcess /@ summaries, {{"MeanMarker", 1, Red}}, 
 ChartStyle -> 97,
 Method -> {"BoxRange" -> boxRange},  
 ChartElementFunction -> "GlassBoxWhisker"]

enter image description here

Update: An alternative way to add mean markers is to use a custom ChartElementFunction:

ClearAll[ceF]
ceF[f_: "BoxWhisker"] := {ChartElementData[f][##], PointSize[Large], 
    If[#3 === {}, {}, Point@Thread[{Mean[#[[1]]], #3[[1]]}]], White, 
    Thick, Dashed, Line[Thread[{#[[1]], #2[[1]]}]]} &;

BoxWhiskerChart[summaries, 
 ChartStyle -> 98, 
 Method -> {"BoxRange" -> boxRange}, 
 ChartElementFunction -> ceF["GlassBoxWhisker"]]

enter image description here

The function ceF can also be used to show outliers by using them as metadata in the first argument of BoxWhiskerChart:

outliers = {{12, 13, 14}, {1, 13, 15}, {12, 13}};
BoxWhiskerChart[Thread[summaries -> outliers], 
 ChartStyle -> 98, 
 Method -> {"BoxRange" -> boxRange}, 
 ChartElementFunction -> ceF["GlassBoxWhisker"]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Brilliant, thank you, now the next part is, if I know that there are three outliers (at the top end of the box and whisker diagram) is it possible to include three points too? $\endgroup$
    – wilsnunn
    Dec 14, 2018 at 20:51
  • $\begingroup$ I actually also have the min and the max, not just the range, so that removes the symmetry issue. $\endgroup$
    – wilsnunn
    Dec 14, 2018 at 20:56
  • 1
    $\begingroup$ @wilsnunn, maybe yo can use Epilog to add outliers. $\endgroup$
    – kglr
    Dec 14, 2018 at 21:51
0
$\begingroup$

Here is a quick and dirty way to do it..

val = {{1, 4, 9/2, 5, 9}, {4, 5, 11/2, 6, 8}};(*min,Q1,Median,Q3,max,*)

col = ColorData[97, #] & /@ Range@Length@val;
fence = 0.05;
boxWidth = 0.1;

Show[Table[Graphics[{Black,

    (*wisker*)Line[{{i, val[[i, 1]]}, {i, val[[i, 5]]}}],
    (*lower fence*)
    Line[{{i - fence, val[[i, 1]]}, {i + fence, val[[i, 1]]}}],

    (*upper fence*)
    Line[{{i - fence, val[[i, 5]]}, {i + fence, val[[i, 5]]}}],

    (*box*)col[[i]], 
    Rectangle[{i - boxWidth, val[[i, 2]]}, {i + boxWidth, 
      val[[i, 4]]}],

    (*median*)Red, 
    Line[{{i - boxWidth, val[[i, 3]]}, {i + boxWidth, val[[i, 3]]}}]

    }, Frame -> True, AspectRatio -> 1/1.2], {i, Length@val}]]

enter image description here

$\endgroup$

Your Answer

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

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