0
votes

I would like to make a grouped bar chart with multiple levels of grouping as shown below -

enter image description here

I would like to have the character 'AR' in the graph. The data for the graph is attached with the post.

Col1    Col2    Col3    Col4

AR                      1
AR      1100    0.5     2
AR      1100    1       3
5       1000    0.5     4
5       1000    1       5
5       1050    0.5     4
5       1050    1       5
5       1100    0.5     4
5       1100    1       5
15      1000    0.5     4
15      1000    1       5
15      1050    0.5     4
15      1050    1       5
15      1100    0.5     4
15      1100    1       5

Data from column 1 to 3 should be used for grouping on the X-Axis and the data on column 4 to be used for the height of the bars.

Is it possible to add prefixes or suffixes to the grouping like 0.5h or 1000 C or 5% ?

2

2 Answers

0
votes
y = [1 2 3 4 5 4 5 4 5 4 5 4 5 4 5];

figure

% hold on

h = bar(y);

xTick = 1:15;

set(gca,'xtick',xTick);

yTick = get(gca,'ytick');

set(gca,'xticklabel',[])

xTickLabel = {{'-\newline';'-';'AR'},{'0.5h';'1100 C';'AR'},{'1h';'1100 C';'AR'},{'0.5h'; '1000C' ;'5%'},{'1h'; '1000C';'5%'},{'0.5h'; '1050C'; '5%'},{'1h'; '1050C'; '5%'},{'0.5h'; '1100C'; '5%'},{'1h'; '1100C'; '5%'},{'0.5h'; '1000C'; '15%'},{'1h'; '1000C'; '15%'},{'0.5h'; '1050C'; '15%'},{'1h'; '1050C'; '15%'},{'0.5h'; '1100C'; '15%'},{'1h'; '1100C'; '15%'}};

for k = 1:length(xTick)

    text(xTick(k),yTick(1)-0.06*(yTick(end)-yTick(1)),xTickLabel{k},'HorizontalAlignment','center')

end

Finally, this is what I have narrowed down to and the result is this - enter image description here

0
votes

Well, in the absence of any other answer, I'll post my attempt (done in Octave). It's not perfect, but it's the best I could do. I put your data in a *.csv file:

enter image description here

I had to create one XTickLabel per bar. Also, because csvread only imports numerical data, I had to manually adjust the XTickLabel for the first 3 bars. There may be a better and cleverer way to do this:

data = csvread('data.csv');
data_str = cell(size(data,1),1);  

for k=1:size(data,1)
  if k<=3
    data_str{k} = sprintf([num2str(data(k,3)) '\n' num2str(data(k,2)) '\nAR']);
  else
    data_str{k} = sprintf([num2str(data(k,3)) '\n' num2str(data(k,2)) '\n' num2str(data(k,1))]);
  end
end
data_str{1} = sprintf(['' '\n' '' '\nAR']);

figure 
hold on
h1 = bar(1:2:size(data,1),data(1:2:end,4));
set(h1,'FaceColor','g','BarWidth',0.4)
legend('D','Location','NorthEast')
h2 = bar(2:2:size(data,1),data(2:2:end,4));
set(h2,'FaceColor','b','BarWidth',0.4)
set(gca,'XTick',1:15)
set(gca,'XTickLabel',data_str)
xlim([0 size(data,1)+1])
ylim([0 max(data(:,4))+1])
ylabel('D')

This gives the following result:

enter image description here