Boxplot divided by color with combined labels

I have a datasetthat I recreated with random numbers, which contains two variables with different combinations and associated Y values. I have different subjects for which I want to build boxplots.
% Unique value of 2 variables
Var1unique = ["Type 1", "Type 2", "Type 3"];
Var2unique = ["Group A" , ...
"Group B", ...
"Group C", ...
"Group D", ...
"Group E"
];
% Create the variable value of the same lenght to put in the table
Var1single = categorical(repelem(Var1unique', 5));
Var2single = categorical(repmat(Var2unique', 3, 1));
Var1 =[]; Var2 = []; Yvalue = [];
for i=1:10 % cycle over number of subjects
Var1 =[Var1; Var1single];
Var2 =[Var2; Var2single];
Yvalue = [Yvalue; rand(15,1)];
end
data = table( Var1, Var2, Yvalue);
LegendLabels = ["(A) Option A" , ...
"(B) Option B", ...
"(C) Option C", ...
"(D) Option D", ...
"(E) Option E", ...
];
AxisLabels = {"A", "B", "C", "D", "E"};
In order to create a boxplot I used the boxchart function and 'GroupByColor' property to generate the following plot. Which is pretty much similar to what I want to generate. For the sake of clarity (since not everyone can see color properly) I want to add xtick labels for individual boxplots within a group. But the boxchart function doesn't allow to add xtickslabels for single boxplots of a group as shown here.
Following this I have tired the function boxplotGroup. None of the other alternatives are precise
So I tried an alternative way to display my plots with the function boxplotGroup but it proves to be very limited since is based on boxplot() properties, which doesn't allow to change color of the BoxFaceColor (I have tried this , this and this but I cannot get it work with boxplotGroup) and use a continuous line for the whiskers.
Here are the two alternatives and the relative results.
Alternative 1:
%% ALTERNATIVE 1
plot1 = figure;
boxchart(reordercats(data.Var1, Var1unique), ...
data.Yvalue, ...
'GroupByColor', reordercats(data.Var2, Var2unique), ...
'BoxWidth',0.5);
% Assign Colors
colororder([[0 0.4470 0.7410]; ...
[0.8500 0.3250 0.0980]; ...
[0.9290 0.6940 0.1250]; ...
[0.4940 0.1840 0.5560]; ...
[0.4660 0.6740 0.1880] ...
] )
% Separate group with lines
for iDivide = 0:height(gca().XAxis.Categories)
line([iDivide+0.5, iDivide+0.5],[min(ylim),max(ylim)],'Color','black','LineStyle','--', 'LineWidth', 1)
end
set(findobj(gcf,'type','axes'),'FontName','Helvetica','FontSize',10, ...
'FontWeight','Bold', 'LineWidth', 1.5);
set(gca,'LineWidth',2, ...
'YGrid','on', ...
'GridLineWidth', 1);
l = legend([LegendLabels, '', '', '', '' ], 'Location','eastoutside');
Alternative 2:
%% Alternative 2
colorGroup = [[0 0.4470 0.7410]; ...
[0.8500 0.3250 0.0980]; ...
[0.9290 0.6940 0.1250]; ...
[0.4940 0.1840 0.5560]; ...
[0.4660 0.6740 0.1880] ...
];
colors = repmat(colorGroup, 3, 1);
% Reshape data
datacell = cell(1, numel(Var2unique));
for var2Idx = 1:numel(Var2unique)
Idx1 = (data.Var1 == Var1unique(1) & data.Var2 == Var2unique(var2Idx));
Idx2 = (data.Var1 == Var1unique(1) & data.Var2 == Var2unique(var2Idx));
Idx3 = (data.Var1 == Var1unique(3) & data.Var2 == Var2unique(var2Idx));
datacell{1,var2Idx} = [table2array(data(Idx1, 3)), table2array(data(Idx2, 3)), table2array(data(Idx3, 3))];
end
plot2 = figure;
boxplotGroup(datacell,'groupLines', true , 'interGroupSpace',2, ...
'primaryLabels', AxisLabels, ...
'secondaryLabels',{'Type 1', 'Type 2', 'Type 3'}, ...
'Colors', colorGroup, ...
'Symbol', "o");
set(gca,'LineWidth',2, ...
'YGrid','on', ...
'GridLineWidth', 1);
set(findobj(gcf,'type','axes'),'FontName','Helvetica','FontSize',10, ...
'FontWeight','Bold', 'LineWidth', 1.5);
l = legend([featureTypeLegend, '', '', '', '' ], 'Location','northwest');

 Accepted Answer

I gather that you want to have the boxes filled as in Alternative 1 and the x-ticks and labels as in Alternative 2.
See if using the option 'BoxStyle','filled' in boxplotGroup is sufficient:
% Unique value of 2 variables
Var1unique = ["Type 1", "Type 2", "Type 3"];
Var2unique = ["Group A", "Group B", "Group C", "Group D", "Group E"];
% Create the variable value of the same length to put in the table
Var1single = categorical(repelem(Var1unique.', 5));
Var2single = categorical(repmat(Var2unique.', 3, 1));
Var1 = repmat(Var1single,10,1);
Var2 = repmat(Var2single,10,1);
Yvalue = rand(150,1);
data = table( Var1, Var2, Yvalue);
LegendLabels = [ ...
"(A) Option A", ...
"(B) Option B", ...
"(C) Option C", ...
"(D) Option D", ...
"(E) Option E", ...
];
AxisLabels = {"A", "B", "C", "D", "E"};
%% Alternative 2
colorGroup = [ ...
0 0.4470 0.7410; ...
0.8500 0.3250 0.0980; ...
0.9290 0.6940 0.1250; ...
0.4940 0.1840 0.5560; ...
0.4660 0.6740 0.1880; ...
];
colors = repmat(colorGroup, 3, 1);
% Reshape data
datacell = cell(1, numel(Var2unique));
for var2Idx = 1:numel(Var2unique)
Idx1 = (data.Var1 == Var1unique(1) & data.Var2 == Var2unique(var2Idx));
Idx2 = (data.Var1 == Var1unique(1) & data.Var2 == Var2unique(var2Idx));
Idx3 = (data.Var1 == Var1unique(3) & data.Var2 == Var2unique(var2Idx));
datacell{1,var2Idx} = [table2array(data(Idx1, 3)), table2array(data(Idx2, 3)), table2array(data(Idx3, 3))];
end
plot2 = figure;
boxplotGroup(datacell,'groupLines', true , 'interGroupSpace',2, ...
'primaryLabels', AxisLabels, ...
'secondaryLabels',{'Type 1', 'Type 2', 'Type 3'}, ...
'Colors', colorGroup, ...
'Symbol', "o", 'BoxStyle','filled');
set(gca,'LineWidth',2, ...
'YGrid','on', ...
'GridLineWidth', 1);
set(findobj(gcf,'type','axes'),'FontName','Helvetica','FontSize',10, ...
'FontWeight','Bold','LineWidth', 1.5);

4 Comments

Lorenzo
Lorenzo on 23 Oct 2024
Edited: Lorenzo on 23 Oct 2024
Thank you @Voss for the quick response, yes you get what I was looking for, but I already tried with BoxStyle property, the boxplot are really thin and less readable. I was looking for a solution like the Alternative 1 fill, maybe I'm asking too much and a custom group and boxplot plot or an svg retouch could be quicker solutions. Thanks Again.
Here's how you can create patches to fill in the boxes, using Alternative 2:
% Unique value of 2 variables
Var1unique = ["Type 1", "Type 2", "Type 3"];
Var2unique = ["Group A", "Group B", "Group C", "Group D", "Group E"];
% Create the variable value of the same length to put in the table
Var1single = categorical(repelem(Var1unique.', 5));
Var2single = categorical(repmat(Var2unique.', 3, 1));
Var1 = repmat(Var1single,10,1);
Var2 = repmat(Var2single,10,1);
Yvalue = rand(150,1);
data = table( Var1, Var2, Yvalue);
LegendLabels = ["(A) Option A" , ...
"(B) Option B", ...
"(C) Option C", ...
"(D) Option D", ...
"(E) Option E", ...
];
AxisLabels = {"A", "B", "C", "D", "E"};
%% Alternative 2
colorGroup = [ ...
0 0.4470 0.7410; ...
0.8500 0.3250 0.0980; ...
0.9290 0.6940 0.1250; ...
0.4940 0.1840 0.5560; ...
0.4660 0.6740 0.1880; ...
];
colors = repmat(colorGroup, 3, 1);
% Reshape data
datacell = cell(1, numel(Var2unique));
for var2Idx = 1:numel(Var2unique)
Idx1 = (data.Var1 == Var1unique(1) & data.Var2 == Var2unique(var2Idx));
Idx2 = (data.Var1 == Var1unique(1) & data.Var2 == Var2unique(var2Idx));
Idx3 = (data.Var1 == Var1unique(3) & data.Var2 == Var2unique(var2Idx));
datacell{1,var2Idx} = [table2array(data(Idx1, 3)), table2array(data(Idx2, 3)), table2array(data(Idx3, 3))];
end
plot2 = figure;
h = boxplotGroup(datacell,'groupLines', true , 'interGroupSpace',2, ...
'primaryLabels', AxisLabels, ...
'secondaryLabels',{'Type 1', 'Type 2', 'Type 3'}, ...
'Colors', colorGroup, ...
'Symbol', "o");
for ii = 1:numel(h.boxplotGroup)
ch = h.boxplotGroup(ii).Children;
ch = ch(strcmp(get(ch,'Tag'),'Box'));
for jj = 1:numel(ch)
patch(ch(jj).XData,ch(jj).YData,ch(jj).Color,'FaceAlpha',0.3)
end
end
set(gca,'LineWidth',2, ...
'YGrid','on', ...
'GridLineWidth', 1);
set(findobj(gcf,'type','axes'),'FontName','Helvetica','FontSize',10, ...
'FontWeight','Bold', 'LineWidth', 1.5);
Lorenzo
Lorenzo on 26 Oct 2024
Edited: Lorenzo on 26 Oct 2024
Thank you @Voss, yes this was what I was looking for. I've already tired with patch but I wasn't able to get the color order right.
I would be nice to have also the whiskers in solid black line, and a coherent legend. I will figure it out and post here the solution.
Thanks Again
Sounds good. I look forward to seeing the completed solution.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024a

Asked:

on 23 Oct 2024

Commented:

on 7 Nov 2024

Community Treasure Hunt

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

Start Hunting!