Reorder x-axis bar graph categorical

94 views (last 30 days)
Mark Crook-Rumsey
Mark Crook-Rumsey on 27 Nov 2019
Edited: Adam Danz on 2 Dec 2019
Hi all,
I have viewed some of the other suggestions on here, but none have worked for me and I'm unsure where I am going wrong. I am trying to reorder a bar graph to be in the order I have specified in my variable cats.
errlow_ERP = [young_mid_parietal_PMconcept_stderr_ERP old_mid_parietal_PMconcept_stderr_ERP MCI_mid_parietal_PMconcept_stderr_ERP];
ERP_mean = [young_mid_parietal_PMconcept_mean_ERP old_mid_parietal_PMconcept_mean_ERP MCI_mid_parietal_PMconcept_mean_ERP];
errhigh_ERP = [young_mid_parietal_PMconcept_stderr_ERP old_mid_parietal_PMconcept_stderr_ERP MCI_mid_parietal_PMconcept_stderr_ERP];
cats = categorical({'Young','Older adult','MCI'});
subplot(3,4,12)
hold on;
p = bar(cats,ERP_mean,...
'FaceColor',[0.75,0.75,0.75],...
'EdgeColor','k',...
'LineWidth',1.5,...
'BaseValue',0);
baseline = p.BaseLine;
er = errorbar(cats,ERP_mean,errlow_ERP,errhigh_ERP,'.','vertical');
set(gca,'LineWidth',1,'FontSize',14)
ylim([0 8.5])
er.LineWidth = 1.5;
er.Color = 'k';
er.MarkerSize = 1;
xlabel('Group','FontSize',16)
ylabel('Amplitude in \muV','FontSize',16)
Any advice is greatly appreciated!

Answers (1)

Adam Danz
Adam Danz on 27 Nov 2019
Edited: Adam Danz on 2 Dec 2019
cats = categorical({'Young','Older adult','MCI'});
cats = reordercats(cats,{'Young','Older adult','MCI'});
p = bar(cats, ERP_mean, . . .);
I know it doesn't look intuitive because the order specified by reordercats is the same as the original order but this sets that order in stone.

Community Treasure Hunt

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

Start Hunting!