2
$\begingroup$

I work with BoxWhiskerCharts at the moment. They code looks like this:

data = RandomInteger[5, 50];
chart = BoxWhiskerChart[{data},
{{"MedianMarker", 1, Black},{"MeanMarker", 1, White}, {"Outliers"}},
ChartLabels -> Placed[{"data"}, Above]]

Now I would like to label the whiskers, both markers (mean and median), and the minimum/maximum of the box with the corresponding values. In the end it should look similar to this (quick and dirty) Excel example:

enter image description here

What I want is circled red in the picture. I do not need to label each and every data point. How can I accomplish that with Mathematica?

$\endgroup$
3
$\begingroup$

You can use a custom ChartElementFunction to add labels at desired positions:

ClearAll[cef]
cef[df_: "BoxWhisker"][off_: 5] := Module[{x = #[[1]],
   q = Quartiles @ #2, m = Round[Mean @ #2, .1], minmax = MinMax@#2}, 
 {ChartElementDataFunction[df][##], Darker @ Charting`ChartStyleInformation["Color"], 
  FontWeight -> Bold, FontSize -> Medium, 
  Text[m, Offset[{-off, 0}, {x[[1]], m}], {1, 0}], 
  Text[#, Offset[{off, 0}, {x[[2]], #}], {-1, 0}] & /@ q, 
  Text[#, Offset[{-off, 0}, {x[[2]], #}], {-1, 0}] & /@ minmax}] &

Examples:

BoxWhiskerChart[{data}, {{"MedianMarker", 1, Black}, {"MeanMarker", 1,
    White}, {"Outliers"}}, ChartElementFunction -> cef[][]]

enter image description here

BoxWhiskerChart[{{data, 1 + data}, {2 + data, 3 + data}},
 {{"MedianMarker", 1, Black}, {"MeanMarker", 1, White}, {"Outliers"}}, 
 ChartElementFunction -> cef["GlassBoxWhisker"][],  BarSpacing -> {Large, Large}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you, that works like a charm. Quick note for other users like me since it took me some minutes: of course you have to adjust the rounding parameter (m = Round[Mean @ #2, .1]) to your data. Im working with quite small numbers which made it look like it is messing with the plot range. $\endgroup$ – phoenix-bs Nov 25 '18 at 20:53

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.