How can I indicate significance in boxplots?

Hi guys,
I would like to compare data of several groups using boxplots. Therefore, I put 4 boxes beside each other. Now I am looking for a possibility to display asterisks over the boxplots (* for p<0.05, * * for p<0.01, ...) and also brackets to make clear which groups are significantly different at which level. To see what I am hoping to be able to do you can go on that page: http://www.frontiersin.org/files/Articles/1379/fnsys-04-00033-r1/image_m/fnsys-04-00033-g004.jpg (this is not my figure, but I would like to do something similar).
Do you have any idea which commands or toolboxes could be helpful in this case?
Thanks a lot!

 Accepted Answer

I did this recently with Creating sigstar in bar graph.
For your box plot, experiment with this to get the result you want:
d = randi(10, 20, 3);
figure(1)
boxplot(d)
yt = get(gca, 'YTick');
axis([xlim 0 ceil(max(yt)*1.2)])
xt = get(gca, 'XTick');
hold on
plot(xt([2 3]), [1 1]*max(yt)*1.1, '-k', mean(xt([2 3])), max(yt)*1.15, '*k')
hold off
The ‘xt’ and ‘yt’ variables get the x-tick and y-tick values, and are then used to do the ‘sigline’ and asterisk calculations on. The axis call simply creates room at the top of the plot box for the line and asterisk.

4 Comments

Hi!
Thank you for your code and the hint with the hg2! Unfortunately, I am still working with R2013a... Maybe this is the reason for the following error: Index exceeds matrix dimensions. Error in significantbars (line 8) plot(xt([2 3]), [1 1]*max(yt)*1.1, '-k', mean(xt([2 3])), max(yt)*1.15, '*k')
But thank you! If I somehow manage to make it run, you will hear from me.
My pleasure!
That code should work on R2013a. What size is ‘xt’ in your code?
Since you’re using it in a subroutine, after this line:
xt = get(gca, 'XTick');
put this line (without a terminal semicolon):
size_xt = size(xt)
You may have to substitute 'XTick' with 'XData' in the ‘xt’ assignment. Try that also. (It’s difficult to keep track of the version changes sometimes. It makes ‘Answers’ something of a challenge.)
One thing I also discovered is you may have to adjust the y-axis range in the axis call. For example, I'm working with data that range from -23 to +18, so I rewrote the axis call as
axis = ([xlim -25 ceil(max(yt)*1.2)])
It seems these commands do not work for boxchart command. Is there any other way to do the same for boxchart?

Sign in to comment.

More Answers (1)

Hi again!
Thanks a lot for your answer! It was really helpful. I had to insert set(gca, 'xtick', 1:3); because my boxplots did not have xticks.
Great!
Best regards

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!