MATLAB Answers

How to plot using "Hierarchically grouped boxplot"?

67 views (last 30 days)
Rita on 26 Sep 2016
Commented: Hydro on 28 Dec 2017
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 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!


Sign in to comment.

Answers (1)

Kelly Kearney
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(, 'color', 'b');
set([h1.lwhis h1.uwhis], 'linestyle', '-', 'color', 'b');
set(, 'color', 'b');
set(h1.out, 'marker', '.', 'color', 'b');
set(, '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(, 'color', 'r');


Show 1 older comment
Kelly Kearney
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);
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.
x1 = {'RCP45_1' 'RCP45_2' 'RCP45_3' 'RCP45_4' 'RCP45_5'}
x2 = {'RCP85_1' 'RCP85_2' 'RCP85_3' 'RCP85_4' 'RCP85_5'}
% the following should come on the right side of the figure
Any help would be highly appreciated.

Sign in to comment.

Sign in to answer this question.