I want to plot a grouped bar graph like in the figure below:
I've included a mock-up of the data I'm using:
I want to group the bars by SampleSite
, label them by SampleName
, and color them by Category
. I've tried the following code, and while it gives me something close to what I'm looking for, I'm unsure how to get the data grouped and colored by the column values.
y1= samples(MockUp_Data.SampleSite== "Site1", :);
y2= samples(MockUp_Data.SampleSite== "Site2", :);
y3= samples(MockUp_Data.SampleSite== "Site3", :);
y= [y1;y2;y3];
bar(y.Data)
set(gca, 'xticklabels', char(y.SampleName), 'fontweight', 'bold', 'fontsize', 12)
pivot
your current "long" table into a "wide" table:SampleSite
(group) per rowCategory
(color) per columnNow we have x in column 1, y in columns
2:end
, and legend entries in headers2:end
:Note that your text description and mock figure are flipped (former is grouped by
SampleSite
, latter is grouped byCategory
). To follow your mock figure, flip the rows and columns while pivoting: