필터 지우기
필터 지우기

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

조회 수: 29 (최근 30일)
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
Adam Danz
Adam Danz 2023년 3월 23일
편집: Adam Danz 2023년 3월 23일
@Dave B's answer in the thread you mentioned seems to show the solution. Is there a section that you're stuck on?

댓글을 달려면 로그인하십시오.

채택된 답변

Shree Charan
Shree Charan 2023년 5월 5일
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
Dave B
Dave B 2023년 5월 5일
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 2023년 5월 7일
Shree and Dave, thank you both so much, both codes works beautifully and I am so greatful!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by