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

74 views (last 30 days)
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 Comment
Adam Danz
Adam Danz on 23 Mar 2023
Edited: Adam Danz on 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?

Sign in to comment.

Accepted Answer

Shree Charan
Shree Charan on 5 May 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 Comments
Dave B
Dave B on 5 May 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')

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!