Filter löschen
Filter löschen

How to connect individual scatter points across grouped bars?

6 Ansichten (letzte 30 Tage)
Prashanti Ganesh
Prashanti Ganesh am 22 Feb. 2022
Beantwortet: Voss am 25 Feb. 2022
Hello All,
I have plotted a grouped bar graph which is overlayed with scatter points of each participant. However, I want to plot a line connecting one scatter point on the first bar to a corresponding scatter point on the next bar. Is there a way to do so?
I have attached the code and figure. Any help is appreciated.
Thanks,
Prashanti
figure
% subplot(1,2,2)
h=bar([mean(y(x==1,:));mean(y(x==2,:));mean(y(x==3,:));mean(y(x==4,:))]'');
hold on
h(1).FaceColor= color(1,:);
h(2).FaceColor= color(2,:);
hold on
ngroups = size(y_SEM, 1); % y_SEM is avg arranged as single means
nbars = size(y_SEM, 2);
% Calculating the width for each bar group
groupwidth = min(0.8, nbars/(nbars + 1.5));
for i = 1:nbars
a = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
errorbar(a, y_SEM(:,i),SEM(:,i), 'k', 'linestyle', 'none','LineWidth',2);
end
hold on
ylabel('Average Economic Performance')
hold on
set(gca, 'Color', 'None')
hold on
x = t.x;
hold on
scatter(repmat(h(2).XEndPoints(1), sum(x==1),1), y(x==1,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
scatter(repmat(h(2).XEndPoints(2), sum(x==2),1), y(x==2,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
scatter(repmat(h(2).XEndPoints(3), sum(x==3),1), y(x==3,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
scatter(repmat(h(2).XEndPoints(4), sum(x==4),1), y(x==4,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
hold on
scatter(repmat(h(1).XEndPoints(1), sum(x==1),1), y(x==1,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
scatter(repmat(h(1).XEndPoints(2), sum(x==2),1), y(x==2,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
scatter(repmat(h(1).XEndPoints(3), sum(x==3),1), y(x==3,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
scatter(repmat(h(1).XEndPoints(4), sum(x==4),1), y(x==4,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
ylim([0.2,1])
ylabel('Economic performance','FontName','Times New Roman','FontSize',10)
hold on
set(gca,'xticklabel',xtick,'FontName','Times New Roman','FontSize',10)
hold on
xlabel('Condition','FontName','Times New Roman','FontSize',10)
hold on
box off

Akzeptierte Antwort

Voss
Voss am 25 Feb. 2022
Here's an example with non-realistic data (because I didn't feel like reverse engineering and reproducing the code you ran to generate all the variables you're using in the plots, so I just made up some random data), but maybe it gets the point across:
% random data:
n_pts = 10;
x = repmat(1:4,1,n_pts).';
y = rand(size(x,1),2);
color = [0.8 0 0; 0.5 0.2 0];
y_SEM = rand(4,2);
SEM = 0.002*rand(4,2);
figure
% subplot(1,2,2)
h=bar([mean(y(x==1,:));mean(y(x==2,:));mean(y(x==3,:));mean(y(x==4,:))]);
hold on
h(1).FaceColor= color(1,:);
h(2).FaceColor= color(2,:);
hold on
ngroups = size(y_SEM, 1); % y_SEM is avg arranged as single means
nbars = size(y_SEM, 2);
% Calculating the width for each bar group
groupwidth = min(0.8, nbars/(nbars + 1.5));
for i = 1:nbars
a = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
errorbar(a, y_SEM(:,i),SEM(:,i), 'k', 'linestyle', 'none','LineWidth',2);
end
hold on
ylabel('Average Economic Performance')
hold on
set(gca, 'Color', 'None')
hold on
% x = t.x;
hold on
% scatter(repmat(h(2).XEndPoints(1), sum(x==1),1), y(x==1,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% scatter(repmat(h(2).XEndPoints(2), sum(x==2),1), y(x==2,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% scatter(repmat(h(2).XEndPoints(3), sum(x==3),1), y(x==3,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% scatter(repmat(h(2).XEndPoints(4), sum(x==4),1), y(x==4,2),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% hold on
% scatter(repmat(h(1).XEndPoints(1), sum(x==1),1), y(x==1,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% scatter(repmat(h(1).XEndPoints(2), sum(x==2),1), y(x==2,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% scatter(repmat(h(1).XEndPoints(3), sum(x==3),1), y(x==3,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% scatter(repmat(h(1).XEndPoints(4), sum(x==4),1), y(x==4,1),15,"black",'x','MarkerEdgeColor','k','XJitter','randn','XJitterWidth',.2)
% instead of scatter plots, make lines (i.e., connect what would
% be the scattered markers with line segments).
% also, use the underlying variables instead of properties of the graphics
% objects (i.e., bars in this case) that were created from those variables.
for ii = 1:n_pts
idx = (ii-1)*4+(1:4);
line( ...
'Parent',gca(), ...
'XData',x(idx)-0.15, ...
'YData',y(idx,1), ...
'Color',color(1,:), ...
'Marker','x', ...
'MarkerEdgeColor','k');
line( ...
'Parent',gca(), ...
'XData',x(idx)+0.15, ...
'YData',y(idx,2), ...
'Color',color(2,:), ...
'Marker','x', ...
'MarkerEdgeColor','k');
end
% ylim([0.2,1])
ylabel('Economic performance','FontName','Times New Roman','FontSize',10)
hold on
set(gca,'xticklabel',1:4,'FontName','Times New Roman','FontSize',10)
hold on
xlabel('Condition','FontName','Times New Roman','FontSize',10)
hold on
box off

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by