Boxchart function gives empty group

5 次查看(过去 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

采纳的回答

Cris LaPierre
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)
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 个评论
Cris LaPierre
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)
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);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by