Reorder x-axis bar graph categorical
60 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Adam Danz
on 27 Nov 2019
Edited: Adam Danz
on 20 Sep 2023
Starting in MATLAB R2023b, bar accepts string arrays in addition to categoricals in the x-argument. The bar order will maintain the string order.
str = ["Young","Older adult","MCI"];
y = 1:3;
bar(str, y)
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.
figure
cats = categorical({'Young','Older adult','MCI'});
cats = reordercats(cats,{'Young','Older adult','MCI'});
p = bar(cats, y);
0 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!