Question

I am interested in creating a plot like this in Matlab. Plot of Means
(source: 3rs-reduction.co.uk)

By "like this" I mean that I wish to have a two groups, call them A and B, observed twice, pretreatment and post-treatment, where the plot consists of a left group of the box plots of A and B at time pretreatment and the right group consists of boxplots of A and B at time post-treatment, and where the mean of A pre is linked by a line to mean of A post and the mean of B pre is linked by a line to mean of B post. I don't mean that the specific appearance of colors shapes, or lines (rather than boxes) needs to be maintained.

I tried to get there with a boxplot command and using "hold on" but I couldn't quite figure out how to make it all fit together. To get concrete, assume that like in the Matlab boxplot command, where the observations are in rows and the fields are columns, and that the order is ((A,pre),(A,post),(B,pre),(B,post)).

Some example code to build upon:

simlength = 100;
groupmeans = [.1, .2, .2, .4];
groupstddev = [.05, .05, .05, .05];
simholder = randn(simlength,4);
simholder = repmat(groupmeans ,simlength,1) + simholder     .* repmat(groupstddev ,simlength,1);
boxplot(simholder)

If I could stack the results 1 and 3 and results 2 and 4 of that boxplot and draw the lines between the group means I would be quite happy, just not sure how to put all the pieces together.

Thanks!

Was it helpful?

Solution 2

Dan's answer was close (thanks!) but didn't include the formatting details I needed to mimic the above chart. Once I figured out how to do that, I tried editing his to add the required changes but the editors didn't care for my changes. So here here is what I wanted.

y1 = rand(2, 1);           
e1 = rand(2, 1)*.2;       
errorbar(y1, e1, 'o-');  

hold all

%Now repeat for B
y2 = rand(2, 1);
e2 = rand(2, 1)*.2;
errorbar(y2, e2, '^-');
plotrange = [y1-e1;y2-e2;y1+e1;y2+e2];
yplotmin = min(plotrange)* .5;
yplotmax = max(plotrange) * 1.5;

legend({'Control', 'Treatment'})

set(gca,'YLim',[yplotmin yplotmax])
set(gca,'XLim',[.5 2.5])
set(gca,'XTick',1:2)
set(gca,'XTickLabel',{'Pre-treatment', 'Post-treatment'});

Difference in Difference Boxplot-like Plot

OTHER TIPS

How about something along the lines of:

x = rand(2, 1);           % ? maybe just leave out?
y = rand(2, 1);           % [mean(Apre), mean(Apost)]
e = rand(2, 1)*.2;        % ? is this maybe [std(Apre), std(Apost)]
errorbar(x, y, e, 'o-');  % You can leave off the x here if you don't need it

hold all

%Now repeat for B
x = rand(2, 1);
y = rand(2, 1);
e = rand(2, 1)*.2;
errorbar(x, y, e, '^-');

legend({'First series', 'Second Series'})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top