Boxchart function gives empty group
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cris LaPierre
2023-10-27
编辑:Cris LaPierre
2023-10-27
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 个评论
Cris LaPierre
2023-10-27
编辑:Cris LaPierre
2023-10-27
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!