Clear Filters
Clear Filters

Issue with BoxChart in TiledLayout - Axis behavior

3 views (last 30 days)
There's a strange issue happening when I use a tiled layout with BoxChart(), where my code produces the following figure:
But, when I click on one of the y-axis to adjust it slightly, the boxchart for that tile changes as follows (I manually changed the axis on the upper left tile for the "Spinal Cord" boxchart:
After I touch the axis at all, the boxes take up much more space, leaving less white space around my box charts.
I'm using R2023a, and just saw on another thread that the manual manipulation of axis in tiled layouts was actually a bug that's been fixed in R2023b.
I would prefer my plots have less white space and look more similar the second image, but I don't want to have to manually manipulate every axis as I have around 30 values that I'll be making these plots for.
Is there a way to make all of my boxcharts appear similarly to the boxchart for the spinal cord in my second figure above programatically?
When i check the BoxWidth paramter before and after manual manipulation, Matlab tells me the BoxWidth parameter hasn't changed (I set it to 0.85 in my code, shared below just in case):
% set parameters
wodth = 0.08;
bwid = .85;
mark_sz = 14;
ctrl_col = [0 0.4470 0.7410];
ctrl_x = ones(ctrl_ct,1)*0.67;
ctrl_y1 = 1;
ctrl_y2 = ctrl_ct;
symp_col = [0.9290 0.6940 0.1250];
symp_x = ones(symp_ct,1)*1.33;
symp_y1 = ctrl_ct + 1;
symp_y2 = ctrl_ct + symp_ct;
asym_col = [0.8500 0.3250 0.0980];
asym_x = ones(asym_ct,1);
asym_y1 = symp_y2 + 1;
asym_y2 = symp_y2 + asym_ct;
% build figure
f = figure();
t = tiledlayout(2,2);
% tile one - Spinal Cord Boxplots
ax1 = nexttile;
% first plot the boxchart
boxchart(ax1,sc_tbl{:,variable},'GroupByColor',sc_tbl.Treat,...
'BoxWidth',bwid)
hold on
% add swarmchart to show all individual data points
s1 = swarmchart(ctrl_x,sc_tbl{ctrl_y1:ctrl_y2,variable},mark_sz);
s1.XJitterWidth = wodth;
s1.HandleVisibility = 'off';
s1.MarkerEdgeColor = ctrl_col;
s1.MarkerFaceColor = ctrl_col;
s2 = swarmchart(asym_x,sc_tbl{asym_y1:asym_y2,variable},mark_sz);
s2.XJitterWidth = wodth;
s2.HandleVisibility = 'off';
s2.MarkerEdgeColor = asym_col;
s2.MarkerFaceColor = asym_col;
s3 = swarmchart(symp_x,sc_tbl{symp_y1:symp_y2,variable},mark_sz);
s3.XJitterWidth = wodth;
s3.HandleVisibility = 'off';
s3.MarkerEdgeColor = symp_col;
s3.MarkerFaceColor = symp_col;
xticklabels([' '])
xlabel('Spinal Cord')
ylim(y_vals)
hold off
ax2 = nexttile;
boxchart(ax2,ton_tbl{:,variable},'GroupByColor',ton_tbl.Treat,...
'BoxWidth',bwid)
hold on
s1 = swarmchart(ctrl_x,ton_tbl{ctrl_y1:ctrl_y2,variable},mark_sz);
s1.XJitterWidth = wodth;
s1.HandleVisibility = 'off';
s1.MarkerEdgeColor = ctrl_col;
s1.MarkerFaceColor = ctrl_col;
s2 = swarmchart(asym_x,ton_tbl{asym_y1:asym_y2,variable},mark_sz);
s2.XJitterWidth = wodth;
s2.HandleVisibility = 'off';
s2.MarkerEdgeColor = asym_col;
s2.MarkerFaceColor = asym_col;
s3 = swarmchart(symp_x,ton_tbl{symp_y1:symp_y2,variable},mark_sz);
s3.XJitterWidth = wodth;
s3.HandleVisibility = 'off';
s3.MarkerEdgeColor = symp_col;
s3.MarkerFaceColor = symp_col;
xticklabels([' '])
xlabel('Tonsil')
ylim(y_vals)
hold off
ax3 = nexttile;
boxchart(ax3,pon_tbl{:,variable},'GroupByColor',pon_tbl.Treat,...
'BoxWidth',bwid)
hold on
s1 = swarmchart(ctrl_x,pon_tbl{ctrl_y1:ctrl_y2,variable},mark_sz);
s1.HandleVisibility = 'off';
s1.XJitterWidth = wodth;
s1.MarkerEdgeColor = ctrl_col;
s1.MarkerFaceColor = ctrl_col;
s2 = swarmchart(asym_x,pon_tbl{asym_y1:asym_y2,variable},mark_sz);
s2.XJitterWidth = wodth;
s2.HandleVisibility = 'off';
s2.MarkerEdgeColor = asym_col;
s2.MarkerFaceColor = asym_col;
s3 = swarmchart(symp_x,pon_tbl{symp_y1:symp_y2,variable},mark_sz);
s3.XJitterWidth = wodth;
s3.HandleVisibility = 'off';
s3.MarkerEdgeColor = symp_col;
s3.MarkerFaceColor = symp_col;
xticklabels([' '])
xlabel('Pons')
ylim(y_vals)
hold off
ax4 = nexttile;
boxchart(ax4,med_tbl{:,variable},'GroupByColor',med_tbl.Treat,...
'BoxWidth',bwid)
hold on
s1 = swarmchart(ctrl_x,med_tbl{ctrl_y1:ctrl_y2,variable},mark_sz);
s1.XJitterWidth = wodth;
s1.MarkerEdgeColor = ctrl_col;
s1.MarkerFaceColor = ctrl_col;
s1.HandleVisibility = 'off';
s2 = swarmchart(asym_x,med_tbl{asym_y1:asym_y2,variable},mark_sz);
s2.XJitterWidth = wodth;
s2.HandleVisibility = 'off';
s2.MarkerEdgeColor = asym_col;
s2.MarkerFaceColor = asym_col;
s3 = swarmchart(symp_x,med_tbl{symp_y1:symp_y2,variable},mark_sz);
s3.XJitterWidth = wodth;
s3.HandleVisibility = 'off';
s3.MarkerEdgeColor = symp_col;
s3.MarkerFaceColor = symp_col;
xticklabels([' '])
xlabel('Medulla')
ylim(y_vals)
hold off
t.Title.String = title_str;
t.Title.FontWeight = 'bold';
t.Title.FontSize = 14;
t.YLabel.String = ylab_str;
t.YLabel.FontWeight = 'bold';
t.YLabel.FontSize = 14;
lgd = legend;
lgd.Orientation = 'horizontal';
lgd.Layout.Tile = 'North';
t.TileSpacing = 'compact';
I'm guessing that the limited whitespace is due to the bug, and therefore not recreatable with code, but I would really love to reduce the whitespace around my boxcharts so that I can increase the spread of my swarmchart points to better visualize the data here.
Thanks in advance if anyone can help!

Accepted Answer

Baldvin
Baldvin on 14 Dec 2023
I'm investigating the white space, but in the meantime you can create each colored boxchart individually with numeric x-axes and using 'SeriesIndex' to match the swarm colors with the boxcharts.
% Dummy data:
x1=ones(42,1);
x2=2*x1;
x3=3*x1;
y1=randn(42,1);
y2=randn(42,1);
y3=randn(42,1);
wodth = 0.4; % For jitter width
mark_sz = 14;
bwid = .85; % For box width
f=figure;
tiledlayout(2,1)
nexttile
b1=boxchart(x1,y1,'BoxWidth',bwid);
hold on
b2=boxchart(x2,y2,'BoxWidth',bwid);
b3=boxchart(x3,y3,'BoxWidth',bwid);
swarmchart(x1,y1,mark_sz, "filled",'XJitterWidth',wodth,'SeriesIndex',b1.SeriesIndex)
swarmchart(x2,y2,mark_sz, "filled",'XJitterWidth',wodth,'SeriesIndex',b2.SeriesIndex)
swarmchart(x3,y3,mark_sz, "filled",'XJitterWidth',wodth,'SeriesIndex',b3.SeriesIndex)
xticklabels([' '])
xticks(2)
xlabel('Spinal Cord')
hold off
nexttile
b1=boxchart(x1,y1,'BoxWidth',bwid);
hold on
b2=boxchart(x2,y2,'BoxWidth',bwid);
b3=boxchart(x3,y3,'BoxWidth',bwid);
swarmchart(x1,y1,mark_sz, "filled",'XJitterWidth',wodth,'SeriesIndex',b1.SeriesIndex)
swarmchart(x2,y2,mark_sz, "filled",'XJitterWidth',wodth,'SeriesIndex',b2.SeriesIndex)
swarmchart(x3,y3,mark_sz, "filled",'XJitterWidth',wodth,'SeriesIndex',b3.SeriesIndex)
xticklabels([' '])
xticks(2)
xlabel('Pons')
hold off
lgd = legend({'Control','Asymptomatic','Symptomatic'});
lgd.Orientation = 'horizontal';
lgd.Layout.Tile = 'North';
  1 Comment
Gwendolyn Williams
Gwendolyn Williams on 18 Dec 2023
Thank you for the solution! I really appreciate it - So long as my plots have less white space to better visualize the data, then I'm pretty happy and this code does the trick!! Much appreciated!

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!