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.

BoxWhiskerChart automatically uses the same width for each of the boxes in a plot. I would like to force it to use a list of widths (with each width corresponding to a box).

I'm working with time series data, and would like to have the width of each box depend on the duration over which its dataset was collected, regardless of the sampling rate. (For example, a box containing data collected over 2 hours would be twice as wide as that with data over 1 hour).

One way to get a variable width is to use Method and BoxWidth as below. The box widths then scale according to the number of points in each dataset.

data = {{1, 4, 3, 5, 1, 2}, {1, 5, 4, 3, 3, 4, 4, 2, 3, 2, 8}}
durations = {180, 60}

BoxWhiskerChart[data,Method->{"BoxWidth"->"Scaled"}]

But since my sampling rates are variable, this width doesn't reflect the duration of the measurement. Is it possible to apply a list of durations to BoxWidth somehow?

Many thanks!

share|improve this question

1 Answer 1

up vote 4 down vote accepted

This cannot be done easily with BoxWhiskerChart as the method suboption "BoxWidth" only accepts "Scaled" and "Fixed", but for such specialized cases you can always fall back to ChartElementFunction. Here I recreate the default chart element from scratch with all the necessary statistical measures. Note, that the highlighting and tooltip on mouseover are still there!

I rescale the duration length for the i-th data set by dividing it with the max duration value, to yield w.

data = {{1, 4, 3, 5, 1, 2}, {1, 5, 4, 3, 3, 4, 4, 2, 3, 2, 8}};
durations = {180, 60};

f[{{xmin_, xmax_}, {ymin_, ymax_}}, data_, meta_, i_] := Module[
   {w = durations[[i]]/(2 Max@durations), mid = xmin + (xmax - xmin)/2.,
    median, q75, q25},
   {median, q75, q25} = Rescale[{Median@data, Quantile[data, 3/4],
          Quantile[data, 1/4]}, {Min@data, Max@data}, {ymin, ymax}];
   {
    {Antialiasing -> False, GrayLevel@.3, AbsoluteThickness@1.5, 
     Line@{{mid, ymin}, {mid, ymax}}},
    {RGBColor[.798, .825, .968], 
     Rectangle[{mid - w, q25}, {mid + w, q75}]},
    {Antialiasing -> False, White, 
     Line@{{mid - w, median}, {mid + w, median}}},
    {Antialiasing -> False, GrayLevel@.3, AbsoluteThickness@1.5, 
     Line@{{mid - w, ymin}, {mid + w, ymin}}, 
     Line@{{mid - w, ymax}, {mid + w, ymax}}}
    }];

And now compare the default with the modified chart:

{
 BoxWhiskerChart[data, ImageSize -> 300], 
 i = 1;
 BoxWhiskerChart[data, ChartElementFunction -> (f[##, i++] &), ImageSize -> 300]
}

Mathematica graphics

share|improve this answer

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.