Why boxplot grouping variable plotted out of order?

Why is the grouping variable not plotted in the order I've listed it in my code? How do I make it plot sequentially? Please compare the figure with its code.
group = cell(1,2);
doseloop = 192;
for loop = 1:height(newvoldata{doseloop})
if strcmpi('blurred',newvoldata{doseloop}.DistributionType(loop))
switch newvoldata{doseloop}.StudyID(loop)
case {1,2,3,4,5,6,7,8,9,10,13,36} %12
group{2} = [group{2}; 'Grade 0'];
case {14,15,19,20,21,22,23,29,30,31,34,37,38,39} % 14
group{2} = [group{2}; 'Grade 1'];
case {11,12,16,17,18,25,26,27,28,32,33,35,40} % 13
group{2} = [group{2}; 'Grade 2'];
case 24 % 1
group{2} = [group{2}; 'Grade 3'];
otherwise
error('Our code didn''t work as planned.')
end
group{1} = [group{1}; newvoldata{doseloop}.Dose(loop)];
end
end
figure
boxplot(newvoldata{doseloop}.Volume(strcmpi('blurred',newvoldata{doseloop}.DistributionType)),group)
title([organofinterest,' Blurred DVH Comparison for 40 Pt Given GU Toxicity'])
ylabel('Percent Volumes receiving given dose, %')

 Accepted Answer

1 to 10 are Grade 0. 11 is Grade 2. If the labels are to be displayed according to numeric order of the source then that is going to result in Grade 0 then Grade 2 appearing, exactly as happens.
boxplot has no knowledge of how the data was computed. If you had rearranged the case order or scrambled the numeric order of the numbers within the case then the computed data would be the same and the display would be the same.

5 Comments

I expect the default behavior of boxplot to label in the sequence the labels are presented -- and as it turns out, you are correct, that is what is happening.
boxplot makes use of the group cell array:
boxplot(newvoldata{doseloop}.Volume(strcmpi('blurred',newvoldata{doseloop}.DistributionType)),group)
and this second cell's vector presents the labels in the order they're used:
>> group{2}
ans =
120×7 char array
'Grade 0'
'Grade 0'
'Grade 0'
'Grade 0'
'Grade 0'
'Grade 0'
'Grade 2'
'Grade 2'
'Grade 2'
'Grade 2'
'Grade 2'
'Grade 2'
'Grade 0'
'Grade 0'
'Grade 0'
'Grade 1'
'Grade 1'
'Grade 1'
So I would need to sort this grouping variable vector and then use the sorting indices to reorder the data to match, I suppose.
Doing so, this code revision solves the problem:
[group{2},indices] = sort(group{2});
group{1} = group{1}(indices(:,7));
DataToPlot = newvoldata{doseloop}.Volume(strcmpi('blurred',newvoldata{doseloop}.DistributionType));
DataToPlot = DataToPlot(indices(:,7));
figure
boxplot(DataToPlot,group)
Have you considered categorical? You can pass a numeric value in the second parameter to categorical that represents a sorting order.
No. Do you mean categorical arrays? I have no experience with them. It appears I should use them instead of a character vector, since, for example, sorting as above generates an index for each character ('G','r','a',...).
Hi Walter. I have a similar problem. In my boxplot the categories are ordered according to the order of appearance in the categorical array. I would like to select a specific order. What can I do?
Use the syntax
B = categorical(A,valueset,catnames)
The sorting will be by the value you associate in valueset, but the names will be what you give in catnames.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!