Boxplot for multiple categorical data sets
古いコメントを表示
Hi
I want to plot the Boxplots for 3 repeated variables collected for 4 data sets, where each data set has 15x3 values. So i actually want to plot 4 catagories on x-axis, where each catagory will have 3 vertical boxplots.
Can anyone please help me with that.?
I have attache dthe file with name features.
Thanks in advance.
採用された回答
その他の回答 (3 件)
Cris LaPierre
2019 年 10 月 24 日
編集済み: Cris LaPierre
2019 年 10 月 24 日
Not sure what you are hoping it looks like in the end, but here's one way.
load features.mat
data1 = features{1};
data2 = features{2};
data3 = features{3};
data4 = features{4};
subplot(1,4,1)
boxplot(data1)
title('Data 1')
subplot(1,4,2)
boxplot(data2)
title('Data 2')
subplot(1,4,3)
boxplot(data3)
title('Data 3')
subplot(1,4,4)
boxplot(data4)
title('Data 4')

1 件のコメント
Cris LaPierre
2019 年 10 月 24 日
編集済み: Cris LaPierre
2019 年 10 月 24 日
One potentially cool thing is to take advantage of the grouping option (second syntax described in the doc). To do so, I'd recommend getting your data into a table. Create one variable with all the data, one with categorical info on the data set, and one with categorical info on the variable.
% Create column vector of all data
data = [data1(:); data2(:); data3(:); data4(:)];
% Create column vector to indicate dataset
dataSet = categorical([ones(numel(data1),1); ...
ones(numel(data2),1)*2; ...
ones(numel(data3),1)*3; ...
ones(numel(data4),1)*4]);
% Create column vector to indicate the variable
clear var
var(1:length(data1),1) = "Var1";
var(end+1:end+length(data1),1) = "Var2";
var(end+1:end+length(data1),1) = "Var3";
Var = categorical([var;var;var;var]);
% Create a table
testData = table(data,dataSet,Var);

Now you can use a single boxplot command to create the boxplot you describe. You can use multiple grouping variables to organize the data into separate boxplots (enclose them in curly braces). Here, I group first by dataSet, then by Var.
boxplot(testData.data,{testData.dataSet,testData.Var})

The two X-axis labels indicate 1) dataSet and 2) Variable.
If you want to see all the boxplots for a specific variable next to each other, change the order of your grouping variables to first group by Var, then by dataSet.
boxplot(testData.data,{testData.Var,testData.dataSet})

Notice the X-axis labels can still be used to correctly identify each boxplot.
Joana
2019 年 10 月 24 日
0 投票
5 件のコメント
Cris LaPierre
2019 年 10 月 24 日
編集済み: Cris LaPierre
2019 年 10 月 24 日
This is a good time to recommend going through the documentation for boxplot. What is possible is documented there.
For example:
Joana
2019 年 10 月 25 日
編集済み: Cris LaPierre
2019 年 10 月 25 日
Cris LaPierre
2019 年 10 月 25 日
Box plots display the median value. If you want to display mean, you'll have to write additional plot commands. It looks like you already found it, but see this forum post.
If you are new to MATLAB, I'd recommend first completing MATLAB Onramp. It'll walk you through the fundamentals incrementally and interactively.
You also need to update your code and variable names to match the new variable name.
Here's how I might do it.
load 'Data for plot.mat'
nDataSets = 7;
nVars = 3;
nVals = 15;
% Create column vector to indicate dataset
dataSet = categorical([ones(nVars*nVals,1); ...
ones(nVars*nVals,1)*2; ...
ones(nVars*nVals,1)*3; ...
ones(nVars*nVals,1)*4;...
ones(nVars*nVals,1)*5;...
ones(nVars*nVals,1)*6;...
ones(nVars*nVals,1)*7]);
% Create column vector to indicate the variable
clear var
var(1:nVals,1) = "Var1";
var(end+1:end+nVals,1) = "Var2";
var(end+1:end+nVals,1) = "Var3";
Var = categorical([var;var;var;var;var;var;var]);
% Create a table
testData = table(data,dataSet,Var);
h = boxplot(testData.data,{testData.dataSet,testData.Var},...
'ColorGroup',testData.Var,...
'Labels',{'','Data1','','','Data2','','','Data3','','','Data4','','','Data5','','','Data6','','','Data7',''});
% Don't display outliers
ol = findobj(h,'Tag','Outliers');
set(ol,'Visible','off');
%% Add Mean on boxplots
summaryTbl = groupsummary(testData,{'dataSet','Var'},"mean")
hold on
plot(summaryTbl.mean_data, 'dg')
hold off
% Add legend
box_vars = findall(h,'Tag','Box');
legend(box_vars(1:3), {'G1','G2','G3'},'Location','northoutside','Orientation','horizontal');

Joana
2021 年 5 月 21 日
Hudson Vieira Coutinho
2022 年 10 月 12 日
0 投票
Hi friend, take a look on this: https://www.mathworks.com/help/matlab/ref/boxchart.html
its very simple!
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

