2
$\begingroup$

I have two collections of data: xdata and ydata

xdata = { {"A", {1, 2, 5}}, {"B", {5, 7, 2, 2, 5}}, {"C", {3, 2, 5, 7}}};
ydata = { {"A", {7, 2}}, {"B", {7, 2, 5}}, {"C", {6, 7, 3}}};

Using BoxWhiskerChart, I can show xdata or ydata alone:

BoxWhiskerChart[xdata, 
 {{"Whiskers", Dashed}, {"Outliers", None}, {"MedianMarker", 1, Directive[Thick, White]}}, 
 PlotRange -> Automatic, ChartStyle -> 56, ImageSize -> Medium]

alone

Now I want to group the data according to their labels (i.e., A, B, and C). The resulting figure (with the same faked data) is like this (in Python) @ stackoverflow:

group

Thus, my question is:

How to group box-and-whisker in parallel for comparison in BoxWhiskerChart?

In particular, how to show the labels (A, B, and C) in x-axis and how to draw the legends (xdata and ydata)?

$\endgroup$

1 Answer 1

5
$\begingroup$

There's an example exactly like this in the documentation for BoxWhiskerChart. All you need to do is reshape your data into a list of doubles:

xdata = {{"A", {1, 2, 5}}, {"B", {5, 7, 2, 2, 5}}, {"C", {3, 2, 5, 7}}};
ydata = {{"A", {7, 2}}, {"B", {7, 2, 5}}, {"C", {6, 7, 3}}};

labels = {xdata[[All, 1]], None}

xdata = xdata[[All, 2]];
ydata = ydata[[All, 2]];

data = Transpose@{xdata, ydata}

style = Sequence[{
  {"Whiskers", Dashed},
  {"Outliers", None},
  {"MedianMarker", 1, Directive[Thick, White]}},
  ChartStyle -> 56,
  ImageSize -> Medium,
  ChartLegends -> {"x", "y"},
  ChartLabels -> labels];

BoxWhiskerChart[data, style]

BWChart

$\endgroup$
3
  • $\begingroup$ @Karsten Thanks for the edit! $\endgroup$
    – Kris
    Nov 27, 2014 at 12:22
  • $\begingroup$ Would you please help me with this problem also related to BoxWhiskerChart? Thanks in advance. $\endgroup$
    – hengxin
    Dec 19, 2014 at 7:31
  • $\begingroup$ A comment for people who already have functioning BoxWhiskerCharts and just want to add the group labels—you can indeed just do: BoxWhiskerChart[{{{"LabelA",{dataA1,dataA2,...}},{"LabelB",{dataB1,dataB2,...}},...] $\endgroup$ Jan 18, 2018 at 19:27

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.