13
$\begingroup$

When you plot a dataset with BoxWhiskerChart passing over the box in the plot with your mouse pointer shows some specifics about the dataset (min and max values, 1/4, 1/2, and 3/4 quartiles). However, if you use Quartiles or Median to calculate those values, they do not correspond. The values displayed by BoxWhiskerChart are different. Does anyone have an explanation or is it simply a bug? Is it system specific (here 8.0.1 on OSX 10.6.8)?

data = RandomVariate[NormalDistribution[0, 1], 20]
BoxWhiskerChart[data]
Quartiles[data]
Median[data]
$\endgroup$
4
  • $\begingroup$ What happens if you try with an odd number of elements? If I remember well, the Median[]definition changes for lists of odd and even lengths. $\endgroup$ Commented Jun 16, 2012 at 17:31
  • $\begingroup$ @belisarius Good point. With an odd number of elements the median is correct, however, the 1/4 and 3/4 quartiles are still wrong. $\endgroup$
    – VLC
    Commented Jun 16, 2012 at 17:35
  • $\begingroup$ @VLC: that would be "different", not "wrong". Mathematica allows for different parameters for Quantile[], as I have indicated in my answer. $\endgroup$ Commented Jun 16, 2012 at 17:46
  • $\begingroup$ @J.M. you're right (or "different"). I wrote my comment just a sec before your answer appeared. $\endgroup$
    – VLC
    Commented Jun 16, 2012 at 17:48

3 Answers 3

13
$\begingroup$

What BoxWhiskerChart[] is actually using for the quartiles (and the median as well) is Quantile[data, {1/4, 1/2, 3/4}], or more explicitly, Quantile[data, {1/4, 1/2, 3/4}, {{0, 0}, {1, 0}}] (what the docs calls the "inverse empirical CDF" parameters).

Quartiles[data] (and thus also Median[]), on the other hand, is equivalent to Quantile[data, {1/4, 1/2, 3/4}, {{1/2, 0}, {0, 1}}] ("linear interpolation" parameters); these two different ways to specify the quantiles will almost always give different results for a particular data set.

Unfortunately, there does not seem to be an easy way to let BoxWhiskerChart[] know what parameters you prefer to use for Quantile[].

Brett's answer displays the (not-too-well documented) "BoxRange" suboption for Method; as I mentioned in a comment, the compact way to use that along with whatever quantile definition you prefer is BoxWhiskerChart[data, Method -> {"BoxRange" -> (Quantile[#, Range[0, 1, 1/4], {{1/2, 0}, {0, 1}}] &)}], and you can replace {{1/2, 0}, {0, 1}} with your desired parameter set.

$\endgroup$
1
  • 3
    $\begingroup$ With the LabelingFunction Option you could at least show the desired values in the tooltip box $\endgroup$ Commented Jun 16, 2012 at 17:40
14
$\begingroup$

There's an Method option, "BoxRange" that can be used to control the calculation of the box parameters:

BoxWhiskerChart[data, GridLines -> {None, Quartiles[data]},
   Method -> {"BoxRange" -> (Flatten[{Min[#], Quartiles[#], Max[#]}] &)}]

box-whisker chart

The first example under Properties & Relations that shows how the various properties of the chart are calculated, using a concrete dataset.

$\endgroup$
2
  • 3
    $\begingroup$ Method -> {"BoxRange" -> (Quantile[#, Range[0, 1, 1/4], {{1/2, 0}, {0, 1}}] &)} is a bit more compact... $\endgroup$ Commented Jun 17, 2012 at 4:49
  • $\begingroup$ @brett-champion, Do you know what is happening to the "Outliers" when you change the "BoxRange" method? See this post to see, some outliers are being removed when we use a custom box range. $\endgroup$
    – Jason B.
    Commented Feb 24, 2016 at 11:14
4
$\begingroup$

By using J. M.'s answer, this will provide you with a "corrected" tooltip:

label[data_, index_, label_] := 
  Grid[{{Style["max", Bold], 1},           {Style["75%", Bold], #1[[2]]}, 
        {Style["median", Bold], #1[[3]]},  {Style["25%", Bold], #1[[4]]}, 
        {Style["min", Bold], #1[[5]]}}, 
     Dividers -> {{#, #}, {#, #}} &@{Directive[GrayLevel[0.3]], Directive[GrayLevel[0.3]]}, 
     Alignment -> {{Center, ".", {Left}}}, Frame -> GrayLevel[0], 
     BaseStyle -> Directive[AbsoluteThickness[1], Dashing[{}]]] &@
   Join[{Max@data}, Reverse@Quartiles[data], {Min@data}];

data = RandomVariate[NormalDistribution[0, 1], 20]; 

BoxWhiskerChart[data, 
 LabelingFunction -> (Placed[label[##], Tooltip] &), 
 ChartLabels -> Placed[Range[2005, 2009], None]]

box-whisker chart with modified tooltips

$\endgroup$
1
  • $\begingroup$ I'd replace Join[{Max@data}, Reverse@Quartiles[data], {Min@data}] with Quantile[data, Range[1, 0, -1/4], {{1/2, 0}, {0, 1}}] myself... $\endgroup$ Commented Jun 17, 2012 at 4:43

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.