MATLAB: How to sort the boxes produced by “boxchart” by mean

boxboxchartboxplotgroupMATLABmeanordersortedStatistics and Machine Learning Toolbox

I am using the "boxchart" function with two arguments (i.e. "xgroupdata" and "ydata") to create multiple box charts in a plot. However, I want the boxes to be ordered by group mean, that is, the boxes should be sorted from lowest mean to highest mean. Is there any way to achieve this workflow?

Best Answer

The "boxchart" function currently does not provide builtin support for this use case. However, a high-level description of a potential workaround to achieve this workflow is the following:
1. Begin by obtaining the mean summary statistic for each group of data in your dataset. This can be obtained, for instance, by using the "groupsummary" command. See the documentation link below for an example of how this function can be used:
2. Once you obtain the mean of each group, use the "sort" function to sort the array of group means. Use the second output of the "sort" function so that the corresponding group labels can be kept in the correct order (i.e. lowest mean to highest mean). See here:
3. If "xgroupdata" is of "categorical" type, you can use the "reordercats" function with two input arguments to reorder the categories of this array. "xgroupdata" should be the first argument, while the result of step 2 should be the second argument. See here:
Since "boxchart" uses the ordering of categories of a categorical array to determine order along the x-axis, you can use the result of step 3 as a replacement for "xgroupdata" in your call to "boxplot". At this point, your boxes should now be in the desired order.
Alternatively, the Statistics and Machine Learning Toolbox offers a similar function called "boxplot" which allows you to explicitly specify a "GroupOrder" parameter to control the order of the groups. You might find this approach useful if your "xgroupdata" is not of categorical type. See the documentation link below: