make a grouped bar graph with individual data points and std error bars inside the bars

52 vues (au cours des 30 derniers jours)
Dear Matlab,
I have delta and theta power spectrum data for 2 groups of participants. Control group N=11. Patient group N=6 (total 17 subjs).
GOAL: to make a bar graph with:
- individual data points and
- std error bars within the bars.
- I would also like to group them into 2 x-ticks (one for delta, one for theta [each with 2 bars for (1) controls and (2) patients])
- and also delta and theta to each be a different color
IDEAL GRAPH: something very similar to:
What I have to far, please see attached bar graph in standard blue.
I have attached sample data as well. columns 1,3 = controls. columns 2,4 = patient group.
Below is code I have so far:
% load SampleData
figure(8),clf, hold on
b1 = bar([1 2 3 4],mean(logbar,'Omitnan'));%,'FaceColor',[.7 .3 .9]);
errorbar([1,2,3,4],mean(logbar,'Omitnan'),std(logbar,[],'omitnan')/sqrt(17-1),'k.','LineWidth',1);
set(gca,'XTickLabel',{'Delta:Ctrl'; 'Delta:Dra';'Theta:Ctrl'; 'Theta:Dra'},'xtick',[1 2 3 4],'fontweight','bold')
ylabel('Relative Power (dB)','fontweight','bold','fontsize',11)
Thank you so much for any help and time, as always!
Best,
Joanne
  1 commentaire
Adam Danz
Adam Danz le 23 Mar 2023
Modifié(e) : Adam Danz le 23 Mar 2023
@Dave B's answer in the thread you mentioned seems to show the solution. Is there a section that you're stuck on?

Connectez-vous pour commenter.

Réponse acceptée

Shree Charan
Shree Charan le 5 Mai 2023
Hi Joanne,
@Dave B‘s answer in https://www.mathworks.com/matlabcentral/answers/888187-grouped-bar-graph-with-individual-datapoints may be modified to suit the current requirements as follows.
figure(8),clf,
%split the data
ctrl = logbar(:,[1 3]);
dra = logbar(:,[2 4]);
%plot the bar graph
h = bar([mean(ctrl, 'Omitnan'); mean(dra, 'Omitnan')]');
hold on
h(1).FaceColor='r';
h(2).FaceColor='y';
%plot the error bars
errorbar(h(1).XEndPoints,mean(ctrl, 'Omitnan'),std(ctrl, 'Omitnan')/sqrt(17-1),'k.','LineWidth',2)
errorbar(h(2).XEndPoints,mean(dra, 'Omitnan'),std(dra, 'Omitnan')/sqrt(17-1),'k.','LineWidth',2)
%plot the individual points
scatter(repmat(h(1).XEndPoints(1), size(ctrl, 1), 1),ctrl(:, 1),60,'MarkerFaceColor','r','MarkerEdgeColor','k','LineWidth',1)
scatter(repmat(h(1).XEndPoints(2), size(ctrl, 1), 1),ctrl(:, 2),60,'MarkerFaceColor','r','MarkerEdgeColor','k','LineWidth',1)
scatter(repmat(h(2).XEndPoints(1), size(dra, 1), 1),dra(:, 1),60,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1)
scatter(repmat(h(2).XEndPoints(2), size(dra, 1), 1),dra(:, 2),60,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1)
%Give appropriate labels
set(gca,'XTickLabel',["Delta" "Theta"],'fontweight','bold')
ylabel('Relative Power (dB)','fontweight','bold','fontsize',11)
legend(["Ctrl", "Dra"])
hold off
The resulting graph would be as follows
You can further read up on “errorbar” and “repmat” function in the following MATLAB documentation:
  2 commentaires
Dave B
Dave B le 5 Mai 2023
I think I noticed one of your goals was to have two levels of tick labels, that can be a little difficult but a trick with tiledlayout makes it doable. Here's a slight modification of @Shree Charan's answer that adds the second row of labels.
load SampleData
clf
tiledlayout(1,2,'TileSpacing','none')
a1 = nexttile;hold on
b1 = bar([1 2],mean(logbar(:,1:2),'Omitnan'));
scatter(repmat(b1.XEndPoints,size(logbar,1),1), logbar(:,1:2),'filled','SeriesIndex',1,'MarkerEdgeColor','k','LineWidth',1)
errorbar([1 2],mean(logbar(:,1:2),'Omitnan'),std(logbar(:,1:2),[],'omitnan')/sqrt(17-1),'k.','LineWidth',1,'Marker', 'none');
set(a1,'XTickLabel',{'Ctrl'; 'Dra'},'xtick',[1 2],'fontweight','bold')
ylabel('Relative Power (dB)','fontweight','bold','fontsize',11)
xlabel('Delta')
a2=nexttile;hold on
b2 = bar([1 2],mean(logbar(:,3:4),'Omitnan'),'SeriesIndex',2);
scatter(repmat(b2.XEndPoints,size(logbar,1),1), logbar(:,3:4),'filled','SeriesIndex',2,'MarkerEdgeColor','k','LineWidth',1)
errorbar([1 2],mean(logbar(:,3:4),'Omitnan'),std(logbar(:,3:4),[],'omitnan')/sqrt(17-1),'k.','LineWidth',1,'Marker', 'none');
set(a2,'XTickLabel',{'Ctrl'; 'Dra'},'xtick',[1 2],'fontweight','bold','YColor','none')
xlabel('Theta')
linkaxes([a1 a2],'y')
Joanne Hall
Joanne Hall le 7 Mai 2023
Shree and Dave, thank you both so much, both codes works beautifully and I am so greatful!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Errorbars dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by