Is there a way to plot multiple boxplots in a subplot?

31 views (last 30 days)
F(sub,day).Cond(Cond).Targ(TarInd_y,TarInd_x).RT(trcnt(Cond,TarInd_y,TarInd_x))=RT;
Unrecognized function or variable 'sub'.
figure;
for day=1:1
for Cond=1:3
for sub=1:1
for r=1:rows
for c=1:columns
subplot(rows,columns,(r-1)*columns+c), hold on;
boxplot(F(sub,day).Cond(Cond).Targ(r,c).RT)
xticklabels({'Cond One', 'Cond Two', 'Cond Three'});
end
end
end
end
end
%F(1, 1).Cond(1).Targ(1, 1).RT this would be==>%Subject 1 Day one conditions 1:3,Target row 1 column 1(aka top
%left).Reaction time values
So I have some data from an experiment where I had subjects come in on 3 different days for a hand reaching task. In this experiment we had right, left and choice (subject chooses between r and l hand to reach to a target) trials.
I am trying to plot the reaction time values (a vector of 10 values for cond=1 or 2, 20 for cond=3) for all 33 targets (3 rows of 11 columns) for one subject for each different condition (3) as it's own boxplot in a subplot. The code above plots the boxplots on top of one another.
The question is very simple: is there a way to plot cond=1 trials/RT values as the first x-axis tick boxplot and cond2 and cond 3 as second and third tick on the x-axis.
Thanks!

Accepted Answer

Cris LaPierre
Cris LaPierre on 31 Mar 2023
Sure. Turn your condition (0, 1, 3) into a categorical array, and use that to group your data using boxchart.
% tiledlayout(3,11)
C1 = rand(10,1)*10;
cond1 = categorical(zeros(size(C1)));
C2 = rand(10,1)*10;
cond2 = categorical(ones(size(C2)));
C3 = rand(20,1)*10;
cond3 = categorical(ones(size(C3))*3);
C = [C1; C2; C3];
cond = [cond1; cond2; cond3];
boxchart(cond,C)
  5 Comments
Cris LaPierre
Cris LaPierre on 31 Mar 2023
In this situation, I don't think your condition loop is gaining you anything. Just hard code it. Here's one approach. Note that due to the number of graphs, you will want to make the figure fullscreen.
Since boxplots are a visual way to quickly compare different datasets, I might also suggest fixing the ylims of all boxcharts so that you can easily compare them.
tiledlayout(rows,columns)
for day=1:1
for sub=1:1
for r=1:rows
for c=1:columns
C1 = F(sub,day).Cond(1).Targ(r,c).RT;
cond1 = categorical(zeros(size(C1)));
C2 = F(sub,day).Cond(2).Targ(r,c).RT;
cond2 = categorical(ones(size(C2)));
C3 = F(sub,day).Cond(3).Targ(r,c).RT;
cond3 = categorical(ones(size(C3))*3);
C = [C1, C2, C3];
cond = [cond1, cond2, cond3];
nexttile
boxchart(cond,C)
ylim([0,0.6])
end
end
end
end
Leutrim Mehmeti
Leutrim Mehmeti on 1 Apr 2023
Edited: Leutrim Mehmeti on 1 Apr 2023
Okay that worked well. Thank you!
It took me a second to figure out how to turn the grayish color between subplots into white so the graph looks a little cleaner.
t=tiledlayout(rows,columns,'Padding','none');
for day=1:1
for sub=1:1
for r=1:rows
for c=1:columns
C1 = F(sub,day).Cond(1).Targ(r,c).RT;
cond1 = categorical(zeros(size(C1)));
C2 = F(sub,day).Cond(2).Targ(r,c).RT;
cond2 = categorical(ones(size(C2)));
C3 = F(sub,day).Cond(3).Targ(r,c).RT;
cond3 = categorical(ones(size(C3))*3);
C = [C1, C2, C3];
cond = [cond1, cond2, cond3];
nexttile
boxchart(cond,C)
ylim([0,0.60])
h=gca;
S=gcf;
set(gcf,'WindowState','maximized','Color', [1 1 1]);
h.Children.LineWidth=0.3
if c>1
h.YAxis.Visible='off';
end
title(t,'Targets')
xlabel(t,'Conditions')
ylabel(t,'Reaction Time [ms]')
% t.TileSpacing='tight'
% t.Padding='tight'
end
end
end
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!