How to group statistics by 2 variables for plotting?

3 visualizaciones (últimos 30 días)
Ben
Ben el 31 de Oct. de 2022
Comentada: J. Alex Lee el 1 de Nov. de 2022
Hi,
I am exploring the statistics of some clustering. I have an output cell array 'VolcRegion_Clust.mat' with the region (8 different regions) of each record, and which of the 10 clusters it has been assigned to. I am trying to extract statistics for the number of records in each cluster (1-10), grouped by the region. I have tried extensively using loops, tables and other methods to split or summarise the data accordingly (without usings 100s of lines splitting each region/cluster combination individually), but have had no luck so far. Any hints or help would be appreciated - thank you.
Below is a chart made with sample data in excel ('Group_Test.xlsx') illustrating the sort of output I am hoping to get to in the final product.

Respuesta aceptada

J. Alex Lee
J. Alex Lee el 1 de Nov. de 2022
Editada: J. Alex Lee el 1 de Nov. de 2022
are you looking for something like this?
load("VolcRegion_clust.mat")
T = cell2table(VolcType_clust,"VariableNames",["Region","Cluster"]);
T.Region = categorical(T.Region)
T = 66×2 table
Region Cluster ______________________________ _______ Alaska 5 South America 2 Alaska 2 Alaska 2 Iceland and Arctic Ocean 2 Alaska 2 Alaska 2 Alaska 2 Alaska 2 Alaska 2 Alaska 2 Iceland and Arctic Ocean 10 Mediterranean and Western Asia 2 México and Central America 2 South America 2 South America 2
GS = groupsummary(T,["Region","Cluster"])
GS = 19×3 table
Region Cluster GroupCount ______________________________ _______ __________ Africa and Red Sea 2 3 Alaska 2 22 Alaska 5 1 Atlantic Ocean 2 2 Canada and Western USA 2 6 Canada and Western USA 5 1 Canada and Western USA 7 1 Iceland and Arctic Ocean 1 2 Iceland and Arctic Ocean 2 5 Iceland and Arctic Ocean 10 1 Mediterranean and Western Asia 2 2 México and Central America 2 1 México and Central America 9 1 South America 2 12 South America 3 1 South America 4 1
Data = unstack(GS,"GroupCount","Region");
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified.
To use the original INDVAR values as table variable names, set 'VariableNamingRule' to 'preserve'.
Data = sortrows(Data,"Cluster")
Data = 10×9 table
Cluster AfricaAndRedSea Alaska AtlanticOcean CanadaAndWesternUSA IcelandAndArcticOcean MediterraneanAndWesternAsia M_xicoAndCentralAmerica SouthAmerica _______ _______________ ______ _____________ ___________________ _____________________ ___________________________ _______________________ ____________ 1 NaN NaN NaN NaN 2 NaN NaN NaN 2 3 22 2 6 5 2 1 12 3 NaN NaN NaN NaN NaN NaN NaN 1 4 NaN NaN NaN NaN NaN NaN NaN 1 5 NaN 1 NaN 1 NaN NaN NaN 2 6 NaN NaN NaN NaN NaN NaN NaN 1 7 NaN NaN NaN 1 NaN NaN NaN NaN 8 NaN NaN NaN NaN NaN NaN NaN 1 9 NaN NaN NaN NaN NaN NaN 1 NaN 10 NaN NaN NaN NaN 1 NaN NaN NaN
Labels = unique(VolcType_clust(:,1));
figure(1)
bar(categorical(Data.Cluster),Data{:,2:end},'stacked')
legend(Labels)
  2 comentarios
Ben
Ben el 1 de Nov. de 2022
That's great, thanks! The main thing I wasn't aware of was the 'unstack' function, which is really useful! For anyone on this thread wondering how to then plot this up as a stacked bar, my code is below (add this below J's code):
Labels = unique(VolcType_clust(:,1));
figure(1)
bar(categorical(Data.Cluster),Data{:,2:end},'stacked')
legend(Labels)
J. Alex Lee
J. Alex Lee el 1 de Nov. de 2022
@Ben, I appended your code to show the whole thing.
Yea, unstack/stack are ones that I vaguely understand, but never fully understood the arguments - I usually have to experiment :)

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by