adding individual points using scatter in a grouped bar graphs

68 views (last 30 days)
Hi,
I managed to plot a grouped bar graph and error bar using MATLAB forum but I am lost to add individual points in each bar graph.
I found that I can use scatter to add individual data points but I failed. Could anyone help? Thanks.
S1 = [41 55];
S2 = [34 26];
P1 = [64 55 38 41 45 47];
P2 = [90 34 51 54 65 52];
mpi1 = [mean(S1) mean(P1)];
mpi2 = [mean(S2) mean(P2)];
mpi3 = [NaN mean(P3)];
all = [mpi1; mpi2];
S1_err = std(S1,'omitnan')/sqrt(size(S1,2));
Saline2_err = std(S2,'omitnan')/sqrt(size(S2,2));
PFF1_err = std(P1,'omitnan')/sqrt(size(P1,2));
PFF2_err = std(P2,'omitnan')/sqrt(size(P2,2));
mpi1_err = [S1_err P1_err];
mpi2_err = [S2_err P2_err];
all_err = [mpi1_err; mpi2_err];
b = bar(all, 'grouped');
hold on
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(all);
% Get the x coordinate of the bars
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
% Plot the errorbars
errorbar(x',all,all_err,'k','LineStyle','none','LineWidth',2,'Color',[0 0 0],'CapSize',12);
hold on
b(1).FaceColor = 'flat';
b(1).CData(1,:) = [0.7 0.7 0.7];
b(2).FaceColor = 'flat';
b(2).CData(1,:) = [1 0 0];
b(1).CData(2,:) = [0.7 0.7 0.7];
b(2).CData(2,:) = [1 0 0];
alpha(.5)
ca = categorical({'1mpi','2mpi','3mpi'});
ca = reordercats(ca,{'1mpi','2mpi','3mpi'});
set(gca,'xticklabel',ca)
legend('PFF','Saline')

Accepted Answer

Voss
Voss on 18 Mar 2024
S1 = [41 55];
S2 = [34 26];
P1 = [64 55 38 41 45 47];
P2 = [90 34 51 54 65 52];
mpi1 = [mean(S1) mean(P1)];
mpi2 = [mean(S2) mean(P2)];
% mpi3 = [NaN mean(P3)];
mp_all = [mpi1; mpi2];
S1_err = std(S1,'omitnan')/sqrt(size(S1,2));
Saline2_err = std(S2,'omitnan')/sqrt(size(S2,2));
PFF1_err = std(P1,'omitnan')/sqrt(size(P1,2));
PFF2_err = std(P2,'omitnan')/sqrt(size(P2,2));
% mpi1_err = [S1_err P1_err];
% mpi2_err = [S2_err P2_err];
mpi1_err = [S1_err PFF1_err];
mpi2_err = [Saline2_err PFF2_err];
all_err = [mpi1_err; mpi2_err];
b = bar(mp_all, 'grouped');
hold on
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(mp_all);
% Get the x coordinate of the bars
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
% Plot the errorbars
errorbar(x',mp_all,all_err,'k','LineStyle','none','LineWidth',2,'Color',[0 0 0],'CapSize',12);
hold on
b(1).FaceColor = 'flat';
b(1).CData(1,:) = [0.7 0.7 0.7];
b(2).FaceColor = 'flat';
b(2).CData(1,:) = [1 0 0];
b(1).CData(2,:) = [0.7 0.7 0.7];
b(2).CData(2,:) = [1 0 0];
alpha(.5)
ca = categorical({'1mpi','2mpi','3mpi'});
ca = reordercats(ca,{'1mpi','2mpi','3mpi'});
set(gca,'xticklabel',ca)
legend('Saline','PFF')
xd = [ ...
x(1,1)*ones(numel(S1),1); ...
x(1,2)*ones(numel(S2),1); ...
x(2,1)*ones(numel(P1),1); ...
x(2,2)*ones(numel(P2),1); ...
];
yd = [S1(:); S2(:); P1(:); P2(:)];
scatter(xd,yd,'filled','HandleVisibility','off')

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!