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
댓글 수: 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!