How to add space between group of box and whisker bars using boxchart?

13 ビュー (過去 30 日間)
MM
MM 2023 年 8 月 21 日
コメント済み: Voss 2023 年 8 月 23 日
I have data from 4 subjects. I want to add gaps between the red, blue and green bars of each subject using boxchart. Any recommendations would be great!
tbl = MasterData
tbl.SubjectID = categorical(tbl.SubjectID)
Display_Type = {"R","L"};
tbl.TargetDisplay = categorical(tbl.TargetDisplay);
idx = (tbl.TargetDisplay == "L")
tbl_Left_Display = tbl(idx,:)
figure(2);
title('Box and Whisker Plots for Display "L"');
boxchart(tbl_Left_Display.SubjectID,tbl_Left_Display.HorizontalFinalOffset,'GroupByColor',tbl_Left_Display.Color,'Notch','on','BoxWidth',1)
colororder([0 0 1;0 1 0; 1 0 0])
ylim([-8,8])
ylabel('Horizontal Final Offsets')
legend
Example data:
Color HorizontalFinalOffset TargetDisplay SubjectID
_____ _____________________ _____________ _________
{'B'} -1.4175 {'L'} {'JD'}
{'R'} -2.0716 {'L'} {'JD'}
{'G'} 0 {'L'} {'JD'}
{'R'} 2.4858 {'R'} {'JD'}
{'B'} -0.19851 {'R'} {'JD'}
{'G'} 0 {'R'} {'JD'}
{'R'} -2.4937 {'L'} {'JD'}
{'B'} -0.97587 {'L'} {'JD'}
{'G'} 0 {'L'} {'JD'}
{'R'} -0.66174 {'L'} {'JD'}

採用された回答

Voss
Voss 2023 年 8 月 21 日
編集済み: Voss 2023 年 8 月 21 日
First, I make table variables like yours:
d = {'R';'L'};
s = {'JD';'MM';'SH';'YL'};
c = {'B';'G';'R'};
Npts = 1000;
Color = c(randi(numel(c),Npts,1));
HorizontalFinalOffset = 2*randn(Npts,1);
TargetDisplay = d(randi(numel(d),Npts,1));
SubjectID = s(randi(numel(s),Npts,1));
tbl = table(Color,HorizontalFinalOffset,TargetDisplay,SubjectID);
tbl.SubjectID = categorical(tbl.SubjectID);
tbl.TargetDisplay = categorical(tbl.TargetDisplay);
idx = (tbl.TargetDisplay == "L");
tbl_Left_Display = tbl(idx,:)
tbl_Left_Display = 502×4 table
Color HorizontalFinalOffset TargetDisplay SubjectID _____ _____________________ _____________ _________ {'G'} 1.3382 L YL {'G'} 2.2871 L MM {'B'} 1.4812 L YL {'B'} 2.0882 L YL {'B'} -0.053404 L SH {'G'} -0.9785 L MM {'G'} -1.0104 L YL {'B'} 0.63819 L YL {'R'} -0.82647 L MM {'R'} 1.4959 L YL {'R'} -1.5126 L SH {'R'} -0.21446 L YL {'B'} -2.5901 L MM {'G'} 2.9614 L SH {'G'} -1.3147 L YL {'G'} -1.7271 L SH
The boxchart, as you have it already:
figure()
b = boxchart(tbl_Left_Display.SubjectID,tbl_Left_Display.HorizontalFinalOffset,'GroupByColor',tbl_Left_Display.Color,'Notch','on','BoxWidth',1);
colororder([0 0 1; 0 1 0; 1 0 0])
ylim([-8,8])
ylabel('Horizontal Final Offsets')
legend
Now, modifying the gaps between the boxcharts:
Option 1: decreasing 'BoxWidth':
figure()
b = boxchart(tbl_Left_Display.SubjectID,tbl_Left_Display.HorizontalFinalOffset,'GroupByColor',tbl_Left_Display.Color,'Notch','on','BoxWidth',0.5);
colororder([0 0 1; 0 1 0; 1 0 0])
ylim([-8,8])
ylabel('Horizontal Final Offsets')
legend
Option 2: manually setting the XData (adjust as desired):
[s,~,xx] = unique(tbl_Left_Display.SubjectID);
ns = numel(s);
c = {'B';'G';'R'};
nc = numel(c);
figure()
hold on
if nc > 1
offset = 1/max(3,7-nc);
offset = linspace(-offset,offset,nc);
else
offset = 0;
end
width = 1/(2*nc);
for ii = 1:nc
idx = strcmp(tbl_Left_Display.Color,c{ii});
y = tbl_Left_Display.HorizontalFinalOffset(idx);
x = xx(idx)+offset(ii);
boxchart(x,y,'Notch','on','BoxWidth',width);
end
colororder([0 0 1; 0 1 0; 1 0 0])
xlim([0.5 ns+0.5])
xticks(1:ns)
xticklabels(s)
ylim([-8,8])
ylabel('Horizontal Final Offsets')
legend(c)
  2 件のコメント
MM
MM 2023 年 8 月 23 日
Thank you, the second approach was exactly what I needed!
Voss
Voss 2023 年 8 月 23 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by