Boxchart function gives empty group

6 views (last 30 days)
Hi,
I'm trying to make a boxplot for my data. I made a table with my grouping variable (T.Group) and a value that I want to plot (T.CorrVal) (see attachment). I use the following code:
load T
b = boxchart(T.Group, T.CorrVal);
This gives me boxplots for my two groups (CaCl and Sham), but also an extra group called empty. I thought this was because I have some NaN values in my T.CorrVal, but the weird thing is that if I plot the exact same thing with another grouping variable (4 groups: CaClFemale, CaClMale, ShamFemale, ShamMale), like this:
b = boxchart(T.Combi, T.CorrVal);
it does give me the right boxplots, without an extra empty one.
How do I fix this? Thank you in advance

Accepted Answer

Cris LaPierre
Cris LaPierre on 27 Oct 2023
Edited: Cris LaPierre on 27 Oct 2023
This is because your data is categorical. For this datatype, the categories are independent (in some respect) of the values. boxchart is just creating a plot for each category. Your variable just happens to have no 'empty' values, so the plot is showing zero. You can use categories to see what categories the variable accepts.
Before creating the boxchart, try removing all empty categories from your categorical variable using removecats.
% Create a dummy data set
Group = categorical(["CaCl";"Sham";"empty"]);
% Fill Group with values using 2 of the category names
ind = randi(2,100,1);
Group = Group(ind);
CorrVal = rand(size(Group));
T = table(Group,CorrVal)
T = 100×2 table
Group CorrVal _____ ________ CaCl 0.40479 Sham 0.90618 Sham 0.44116 CaCl 0.58259 CaCl 0.42117 Sham 0.30017 CaCl 0.94303 Sham 0.38962 Sham 0.70232 CaCl 0.25015 Sham 0.33066 Sham 0.045428 CaCl 0.93625 CaCl 0.8541 CaCl 0.61066 CaCl 0.97252
% View the number of each group in T
groupsummary(T,"Group",'IncludeEmpty',true)
ans = 3×2 table
Group GroupCount _____ __________ CaCl 50 Sham 50 empty 0
% View the category names in T.Group. Note that empty appears, even though
% there are 0 occurrences.
categories(T.Group)
ans = 3×1 cell array
{'CaCl' } {'Sham' } {'empty'}
% Create a boxchart. empty appears here, too.
boxchart(T.Group,T.CorrVal)
% Now remove empty categories from T.Group and repeat
T.Group = removecats(T.Group);
groupsummary(T,"Group",'IncludeEmpty',true)
ans = 2×2 table
Group GroupCount _____ __________ CaCl 50 Sham 50
categories(T.Group)
ans = 2×1 cell array
{'CaCl'} {'Sham'}
figure
boxchart(T.Group,T.CorrVal)
  2 Comments
Marleen
Marleen on 27 Oct 2023
Thank you so much! It works.
Cris LaPierre
Cris LaPierre on 27 Oct 2023
Edited: Cris LaPierre on 27 Oct 2023
Sorry, missed that you uploaded your data. Here are the steps to use to inspect the categories and then remove the unused category name from your variable.
load T
categories(T.Group)
ans = 3×1 cell array
{'CaCl' } {'Sham' } {'empty'}
T.Group = removecats(T.Group);
categories(T.Group)
ans = 2×1 cell array
{'CaCl'} {'Sham'}
b = boxchart(T.Group, T.CorrVal);

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!