Boxchart function gives empty group
5 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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)
% View the number of each group in T
groupsummary(T,"Group",'IncludeEmpty',true)
% View the category names in T.Group. Note that empty appears, even though
% there are 0 occurrences.
categories(T.Group)
% 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)
categories(T.Group)
figure
boxchart(T.Group,T.CorrVal)
2 Comments
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)
T.Group = removecats(T.Group);
categories(T.Group)
b = boxchart(T.Group, T.CorrVal);
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!