Search code examples
matlabsortingvisualizationmatlab-figureboxplot

Sort means for box plot in matlab


I have a dataset in the following structure:

Data_1 = [10,2,3,4,5,6,7,8,9];
groups_1 = [1,1,2,2,3,3,4,4,5];

Data_2 = [20,12,63,4,5,5,5,2];
groups_2 = [1,1,1,2,3,3,4,5];

boxplot(Data_1,groups_1);boxplot(Data_2,groups_2);

I would like to sort Data_1 according to the mean and group Data_1 and Data_2 next to each other based on the mean of Data_1 into one box plot. My approach was to use grpstats:

Table_1 = array2table([Data_1' groups_1']);
Stats = grpstats(Table_1,'Var2');
sort_Stats = sortrows(Stats,'mean_Var1');

Now I am stuck on how to combine the two data sets and the groups based on the grpstats results efficiently.

Update: I need the following structure as a final goal:

Data_Corrected_1 = [3,4,5,6,10,2,7,8,9];
groups_Corrected_1 = [1,1,2,2,3,3,4,4,5];
boxplot(Data_Corrected_1,groups_Corrected_1);
% Now the box plot is arranged based on the mean

Solution

  • Here is how you can sort your data based on the means of the groups:

    Data_1 = [10,2,3,4,5,6,7,8,9];
    groups_1 = [1,1,2,2,3,3,4,4,5];
    
    [~,ind] = sort(accumarray(groups_1.',Data_1,[],@mean));
    ord1 = sortrows([(1:max(groups_1)).' ind],2);
    % without repelem:
    a = ord1(:,1);
    b = histcounts(groups_1);
    idx(cumsum([1 b(b>0)])) = 1;
    c = a(cumsum(idx(1:find(idx,1,'last')-1)));
    Data_Corrected_1 = sortrows([c groups_1.' Data_1.'],1);
    % with repelem:
    % Data_Corrected_1 = sortrows([repelem(ord1(:,1),histcounts(groups_1))...
    %    groups_1.' Data_1.'],1);
    boxplot(Data_Corrected_1(:,3),Data_Corrected_1(:,1));
    

    sorted_boxplot

    And the same holds for Data_2.