Asked by Rita
on 26 Sep 2016

I would like to draw a boxplot with two sets of data to compare. I am willing to use "Hierarchically grouped boxplot" a function which I found on the mathworks.com in the file exchange. I could just plot one set of my data using this function. I was wondering how I can use this function to plot two sets of data together. I drew the second set of data in red one by hand to show what I am trying to plot!

Answer by Kelly Kearney
on 26 Sep 2016

You can do something similar to this with my boxplot2.m function (sorry, the GitHub page isn't well-documented... I'll correct that shortly). It's specifically designed to plot boxplots using a similar layout as grouped bar plots:

% Some fake data

data1 = randn(6,5,100); % # groups x # boxes in each group x # data points

data2 = randn(6,3,100)+ 5;

x = (2001:2006)';

# Plot boxplots

h1 = boxplot2(data1, x);

h2 = boxplot2(data2, x);

# Change colors from default

set(h1.box, 'color', 'b');

set([h1.lwhis h1.uwhis], 'linestyle', '-', 'color', 'b');

set(h1.med, 'color', 'b');

set(h1.out, 'marker', '.', 'color', 'b');

set(h2.box, 'color', 'r');

set([h2.lwhis h2.uwhis], 'linestyle', '-', 'color', 'r');

set(h2.out, 'marker', '.', 'color', 'r');

set(gcf, 'color', 'w');

export_fig('boxplot2ex', '-png', 'nocrop');

set(h2.med, 'color', 'r');

Kelly Kearney
on 27 Sep 2016

The function will still work with overlap, though it may be a little less legible. Perhaps you want to stagger the boxplots from the two datasets? This example does that, assuming that the two datasets are the same size:

data = randn(6,5,2,100); % #x x #subsets x #datasets x #points

x = (2001:2006)';

tmp = reshape(data, 6, 10, 100);

h = boxplot2(tmp, x, 'barwidth', 0.1);

set([h.lwhis h.uwhis], 'linestyle', '-');

set(h.out, 'marker', '.');

structfun(@(x) set(x(1:2:end,:), 'color', 'b', 'markeredgecolor', 'b'), h);

structfun(@(x) set(x(2:2:end,:), 'color', 'r', 'markeredgecolor', 'r'), h);

Rita
on 28 Sep 2016

Thanks Kelly.

Hydro
on 28 Dec 2017

Hello Kelly, I am trying to adapt your code of boxplot2 for my data but all in vain. The first 5 columns represent data for RCP4.5 while the next 5 RCP8.5. Row 1:360 is monthly data from 2011-2040, and the rest for 2041-2070. I want to boxplot the 2011-2040 of RCP45 vs RCP85 such that column1 of RCP45 is plotted adjacent to column1 of RCP85 and likewise for the rest columns. On the right side of the figure, I want to plot the data same way for the year 2041-2070. Here is what i thought of doing manually and then tried to used your code but couldnt get it.

Fake_Data=rand(720,10);

x1 = {'RCP45_1' 'RCP45_2' 'RCP45_3' 'RCP45_4' 'RCP45_5'}

x2 = {'RCP85_1' 'RCP85_2' 'RCP85_3' 'RCP85_4' 'RCP85_5'}

h1=boxplot2(Fake_Data(1:360,x1));

h2=boxplot2(Fake_Data(1:360,x2));

% the following should come on the right side of the figure

h3=boxplot2(Fake_Data(361:end,x1));

h4=boxplot2(Fake_Data(361:end,x2));

Any help would be highly appreciated.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.