Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]
share|improve this question
    
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. –  belisarius Jun 16 '12 at 17:31
    
@belisarius Good point. With an odd number of elements the median is correct, however, the 1/4 and 3/4 quartiles are still wrong. –  VLC Jun 16 '12 at 17:35
    
@VLC: that would be "different", not "wrong". Mathematica allows for different parameters for Quantile[], as I have indicated in my answer. –  J. M. Jun 16 '12 at 17:46
    
@J.M. you're right (or "different"). I wrote my comment just a sec before your answer appeared. –  VLC Jun 16 '12 at 17:48
add comment

3 Answers

up vote 8 down vote accepted

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.

share|improve this answer
3  
With the LabelingFunction Option you could at least show the desired values in the tooltip box –  belisarius Jun 16 '12 at 17:40
add comment

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.

share|improve this answer
3  
Method -> {"BoxRange" -> (Quantile[#, Range[0, 1, 1/4], {{1/2, 0}, {0, 1}}] &)} is a bit more compact... –  J. M. Jun 17 '12 at 4:49
add comment

By using @JM'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]]

enter image description here

share|improve this answer
    
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... –  J. M. Jun 17 '12 at 4:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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