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

9 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]);
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!
  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 may be modified to suit the current requirements as follows.
%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
%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:

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!

Translated by