x = {rand(100,2), rand(20, 2), rand(150,2)};
boxplotGroup(x)
The lines above will create two groups of three boxplot boxes per group, with space separating the groups.
You can specify the amount of space between groups, the primary labels under each box, and the secondary labels that identify the groups. Group labels can be horizontal, under the x-axis, or vertical, between groups.
Other boxplot properties can be passed to Matlab's boxplot function as well.
NOTE: If you're working with a grouping variable 'g', use the syntax boxplot(x,g) along with the "Group Appearance" options described in Matlab's boxplot() documentation.
https://www.mathworks.com/help/stats/boxplot.html#d118e146984
Also see Kelly Kearney's boxplot2()
https://www.mathworks.com/matlabcentral/fileexchange/59303-boxplot2
Adam Danz (2021). boxplotGroup (https://www.mathworks.com/matlabcentral/fileexchange/74437-boxplotgroup), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Can I add more subgroups in this code?
Thanks, Behzad Navidi. To control the sequence of colors used within each group, set the 'Colors' property (see Matlab's boxplot documentation) but include one additional color than the number of groups to account for the gap between groups. Example:
h = boxplotGroup({rand(100,4), rand(20,4)*.8, rand(1000,4)*1.2}, 'Colors', lines(4));
Alternatively, you can assign colors after the plot is created. Example:
set(h.axis.Children(2).Children,'color', 'm') % sets color for the 2nd set of boxplots
Setting colors *within* each group is not currently supported. Matlab's boxplot function also includes a 'ColorGroup' property that can be used with grouped boxplot created by Matlab's boxplot function but not currently supported by my function.
Please PM me using the [Contact] button in my profile if you have any further questions on this topic.
Dear @Adam Danz, Thank you so much for creating this useful and beneficial function; God bless you. Is it possible to have two different colors for each group (if I have two groups)??
@Reinder, the demo in the function's documentation uses data with varying heights [100x4, 20x4, 1000x4] like you're describing with [n*3, m*3]. If you enter data with a varying number of columns, you'll get an error message that suggests padding the matrices containing fewer columns with NaN values. If that doesn't answer your question, please write to me directly using the [contact] button in my profile.
Hey is possible to use this with unequal amounts of data within each subgroups? As in, let's say I want to have 2 groups with 3 bars in each group, with unequal amount the form of x would be x = [n*3, m*3]; but in this case we can't make a matrix with unequal numbers of columns in each row.
I think the GroupOrder only works for the primary, if i'm seeing it okay?
Easy to use if want to plot multiple boxplot together, especially for writing a paper