Create mean y values by multiple groups in one graph
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dear all, I have a snippet of my panel
id Year y Cat1 Cat2 Cat3 Cat4
1 2000 45 1 0 0 0
1 2001 56 1 0 0 0
1 2002 23 1 0 0 0
1 2003 56 1 0 0 0
1 2004 45 1 0 0 0
2 2000 45 0 1 0 0
2 2001 90 0 1 0 0
2 2002 35 0 1 0 0
2 2003 79 0 1 0 0
2 2004 32 0 1 0 0
Ofcourse my full panel has id=30 and Year=5 .
I want to find the mean values of y by Cat1, up to Cat4, when each Cat takes the value of 1. And then I want to plot a bar chart of these mean values of y against the X axis that will contain the labels Cat1,..., Cat4 in a vertical position.
Is that possible to do that in Matlab?
If it is not clear, please let me know!
Many thanks in advance!
4 comentarios
Mann Baidi
el 2 de Abr. de 2024
Okay, thanks for the clarification.
As per my understanding of the question, you would like to plot the data from Cat1 Cat2.... and y column, right?
Respuestas (2)
Star Strider
el 2 de Abr. de 2024
Editada: Star Strider
el 2 de Abr. de 2024
Try this —
T1 = array2table([1 2000 45 1 0 0 0
1 2001 56 1 0 0 0
1 2002 23 1 0 0 0
1 2003 56 1 0 0 0
1 2004 45 1 0 0 0
2 2000 45 0 1 0 0
2 2001 90 0 1 0 0
2 2002 35 0 1 0 0
2 2003 79 0 1 0 0
2 2004 32 0 1 0 0], 'VariableNames',{'id','Year','y','Cat1','Cat2','Cat3','Cat4'})
Cat_mean = NaN(2, 4);
for k = 1:4
Cat_mean(:,k) = accumarray(1+T1{:,k+3}, 1, [], @(x)mean(T1.y(x)));
end
Cat_mean
There are several ways to do this in MATLAB. This is the approach that I usually use.
for k = 1:4
Catu(:,k) = unique(T1{:,k+3},'stable');
end
Catu
figure
hold on
for k = 1:4
bar(k,Cat_mean(:,k), 'stacked')
end
hold off
xlabel('Cat')
ylabel('Counts')
EDIT — Added bar chart.
.
4 comentarios
Star Strider
el 2 de Abr. de 2024
The easiest way —
T1 = array2table([1 2000 45 1 0 0 0
1 2001 56 1 0 0 0
1 2002 23 1 0 0 0
1 2003 56 1 0 0 0
1 2004 45 1 0 0 0
2 2000 45 0 1 0 0
2 2001 90 0 1 0 0
2 2002 35 0 1 0 0
2 2003 79 0 1 0 0
2 2004 32 0 1 0 0], 'VariableNames',{'id','Year','y','Cat1','Cat2','Cat3','Cat4'})
% Cat_mean = NaN(2, 4);
for k = 1:2%4
Cat_mean(:,k) = accumarray(T1{:,k+3}(T1{:,k+3} ~= 0), 1, [], @(x)mean(T1.y(x)));
end
Cat_mean
for k = 1:4
Catu(:,k) = unique(T1{:,k+3},'stable');
end
Catu
figure
hold on
for k = 1:2%4
bar(k,Cat_mean(:,k), 'stacked')
end
hold off
set(gca,'XTick',(1:4), 'XTickLabel',compose('Cat%d',1:4))
% xlabel('Cat')
ylabel('Counts')
Another option is to use categorical tick labels, however that causes significant problems if you want to add anything numeric later, so I would not recommend it.
.
Adam Danz
el 2 de Abr. de 2024
Editada: Adam Danz
el 2 de Abr. de 2024
Use groupsummary to compute the means of each group. Then plot the results using bar() with categorical x values.
This assumes "1" does not appear in more than 1 group.
% Input table
T = array2table([1 2000 45 1 0 0 0
1 2001 56 1 0 0 0
1 2002 23 1 0 0 0
1 2003 56 0 1 0 0
1 2004 45 0 1 0 0
2 2000 45 0 0 1 0
2 2001 90 0 0 1 0
2 2002 35 0 0 0 1
2 2003 79 0 0 0 1], ...
'VariableNames',{'id','Year','y','Cat1','Cat2','Cat3','Cat4'});
% Compute group means
groups = {'Cat1','Cat2','Cat3','Cat4'};
mu = groupsummary(T,groups,'mean','y')
% Get the group order so ensure the groups are in the same order as the
% means.
groupOrder = varfun(@find,mu(:,1:numel(groups)),'OutputFormat','uniform');
means = mu.mean_y(groupOrder);
% Plot results
bar(categorical(groups),means)
ylabel('mean')
0 comentarios
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!