7
$\begingroup$

I would like a chart showing the median, 1/4, 3/4 quartiles, without the whiskers or fences. how can I achieve this? I cannot figure out how to turn off both the whiskers and the fences in the result of:

BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], 100]]

but doing so would do the trick.

$\endgroup$
  • 1
    $\begingroup$ This seems to be a work around: BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], 100],{{"Fences", 0, None}, {"Whiskers", Opacity[0]}}]. But it seems like there should be a cleaner way. $\endgroup$ – mdeceglie Jun 19 '14 at 17:14
  • $\begingroup$ I recommend you write up your solution as an answer. I think it's quite clean, and, I for one, will up-vote it. $\endgroup$ – m_goldberg Jun 19 '14 at 20:12
7
$\begingroup$

One way to achieve the result is to hide the whiskers and fences:

BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], 100],
  {{"Fences", 0, None}, {"Whiskers", Opacity[0]}}]

enter image description here

This works, but it's a little bothersome to know the objects are hidden rather than absent.

$\endgroup$
4
$\begingroup$

An alternative approach is to remove the unwanted Lines through post-processing:

BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], {5, 100}], 
      ChartStyle -> "Pastel"] /. LineBox[{{{_, _}, {_, _}} ..}] :> {}

enter image description here

BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], {5, 100}], 
     ChartStyle -> "Pastel", BarOrigin -> Left] /. LineBox[{{{_, _}, {_, _}} ..}] :> {}

enter image description here

$\endgroup$
3
$\begingroup$

You can use the ChartElementFunction option to control how the chart is drawn:

BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], 100]]

enter image description here

draw[{{xmin_, xmax_}, {ymin_, ymax_}}, data_, rest___] :=
  With[
   {qs = Quantile[data, {1/4, 1/2, 3/4}]},
   Polygon[{
     {xmin, qs[[1]]}, {xmax, qs[[1]]},
     {Mean@{xmin, xmax}, qs[[2]]},
     {xmin, qs[[3]]}, {xmax, qs[[3]]}
     }]];
BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], 100], ChartElementFunction -> draw]

enter image description here

It would take a bit more code to duplicate the look of the default chart, namely the division between the two rectangles. You'd have to offset from the median when drawing rectangles because otherwise there would be no white space between them.

Your own suggestion is pretty practical, though.

$\endgroup$
3
$\begingroup$

Update 2

It's actually not hard to duplicate the default chart's look with ChartElementFunction. Just build two rectangles with a little offset between them.

boxes[{{xmin_, xmax_}, {ymin_, ymax_}}, data_, rest___] := 
 With[{qPts = Quantile[data, {1/4, 1/2, 3/4}]}, 
   {Rectangle[{xmin, qPts[[1]]}, {xmax, qPts[[2]]}],
    Rectangle[{xmin, qPts[[2]]}, {xmax, qPts[[3]]}],
    {Thick, White, Line[{{xmin, qPts[[2]]}, {xmax, qPts[[2]]}}]}}]

Column[
  (SeedRandom[1];
     BoxWhiskerChart[RandomVariate[NormalDistribution[Sequence @@ #], 100], 
       ChartElementFunction -> boxes,
       ImageSize -> Medium]) & /@ {{0, 1}, {1000, 100}, {0, 1000}}]

boxes

Note: mfvonh brought my attention to a scaling problem in my original post of this answer. I have replaced the non-scaling by a perturbation dy = .03 with an automatically scaling thick line. I tested the new algorithm over a range of means and standard deviations without seeing any problem.

$\endgroup$
  • $\begingroup$ My concern was that this approach doesn't seem to scale well. (E.g., with RandomVariate[NormalDistribution[1000, 100], 100]). I tried using coefficients also but couldn't find a robust solution. Any thoughts? $\endgroup$ – mfvonh Jun 19 '14 at 22:44
  • $\begingroup$ @mfvonh. Thanks for pointing out the scaling problem. I have modified my code to address the issue. $\endgroup$ – m_goldberg Jun 19 '14 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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