How can I adjust boxchart to have different number of groups for different nr of classes?

I would like to change this graph by keeping 4 boxplots for 4 and 6 classes, and then having only blue and red boxplots for 8 classes.

2 Comments

There are a couple different possible approaches here.
One approach would be on the data side, which would be to replace the 8-classes yellow and purple data points with NaN values (or delete them), so that they don't "exist" to be plotted.
A second approach would be on the plotting side, which would be to identify those two boxes, and set their "Visible" property to "Off". (I am not 100% certain one can do this for individual boxes.)
Either way, if you upload the data and code you used to produce the plot, it will be easier to provide a solution.
Thank you for the ideas! I feel like replacing the data with NaN will still "hold the place" for those values, resulting in non-centralized boxes distribution.
I haven't found the possibility to adjust visibility of the boxes separately, unfortunately

Sign in to comment.

 Accepted Answer

Based on my understanding, you want to create a 'boxchart' with a different number of classes for each group. You can accomplish this by utilizing the 'GroupByColor' property of 'boxchart', which allows MATLAB to color the box plots within each group according to the different classes you've specified.
Here’s an example code snippet demonstrating the use of 'GroupByColor':
% Sample data setup
data = randn(100, 1);
group = [ones(30,1); 2*ones(50,1); 3*ones(20,1)]; % Groups 1, 2, 3
class = [randi([1, 2], 30, 1); randi([1, 3], 50, 1); randi([1, 1], 20, 1)]; % Different number of classes per group
% Plotting the boxchart
figure;
boxchart(group, data, 'GroupByColor', class);
xticks([1 2 3]);
xticklabels({'Group 1', 'Group 2', 'Group 3'});
% Adding labels
xlabel('Group');
ylabel('Data');
title('Box chart with different number of groups and classes');
Kindly refer the below documentation to read more on 'GroupByColor' property:
Feel free to attach your data file if you're still encountering issues.

3 Comments

Hi Jatin! Thank you for the help. I was using the GroupByColour option as well so far for the plotting, However, in this appraoch the boxes aren't visually centered in case of group 1 and group 3 (in your example), so by looking it would give an impression that both blue boxes belong to group 2. Do you think there is some way around this issue?
Hello Marietta,
The appearance of the class boxes is determined by the data itself, and there isn't a specific property to increase the distance between groups. However, you can adjust the group locations by modifying the group data, as demonstrated in the code below:
% Sample data setup
data = randn(100, 1);
group = [ones(30,1); 4*ones(50,1); 7*ones(20,1)]; % Groups 1, 2, 3
class = [randi([1, 2], 30, 1); randi([1, 3], 50, 1); randi([1, 1], 20, 1)]; % Different number of classes per group
% Plotting the boxchart
figure;
boxchart(group, data, 'GroupByColor', class);
% Setting x-axis ticks and labels
xticks([1 4 7]);
xticklabels({'Group 1', 'Group 2', 'Group 3'});
% Adding labels
xlabel('Group');
ylabel('Data');
title('Box chart with increased distance between groups');
Great tip! I have achieved the result I wanted. Thank you!

Sign in to comment.

More Answers (0)

Asked:

on 12 Sep 2024

Commented:

on 13 Sep 2024

Community Treasure Hunt

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

Start Hunting!