Stacked Histogram and Further Categorization
5 views (last 30 days)
Show older comments
Hello ! I'm trying to include more information in my histogram to precisely convey to my audience what are the constituents of each bar in a histogram.
Take my animal data example in the file,if i categorize them in 3 different ranges according to their weight I can plot the histogram attached in the image.I now know for the first range (2:5) I have 3 animals in that bracket but I want also to convey that : 1 is a lab animal and 2 are pets.
Either by stacking the plots to indicate that I have 1 lab / 2 pets in the bar or by saying that 50% of the total pets are in this category ! Any ideas on how to achieve such a thing ? Thank you !
figure(1)
r1=readtable("animaldata.xlsx")
edges=[2 5 10 15 ]
histogram(r1.Weight,edges)
0 Comments
Answers (1)
dpb
on 1 Mar 2022
Not too bad a task at all...consider
tA=readtable("animaldata.xlsx");
tA.Animal=categorical(tA.Animal);
tA.Category=categorical(tA.Category);
edges=[2 5 10 15 ];
tA.WtGroup=discretize(tA.Weight,edges);
which leaves you with the following table...
tA =
11×4 table
Animal Weight Category WtGroup
______ ______ ________ _______
Rat 2 Lab 1
Mouse 2 Pet 1
Dog 4 Pet 1
Hippo 8 Wild 2
Horse 9 Pet 2
Cow 10 Lab 3
Cow 11 Wild 3
Hippo 9 Wild 2
Dog 10 Wild 3
Rat 5 Wild 2
Cat 7 Pet 2
Now, let's find out how many of who are in the zoo...
groupsummary(tA,{'WtGroup','Category'})
ans =
6×3 table
WtGroup Category GroupCount
_______ ________ __________
1 Lab 1
1 Pet 2
2 Pet 2
2 Wild 3
3 Lab 1
3 Wild 2
>>
from which you can easily construct your stacked histogram with the resulting group counts by weght group and category.
0 Comments
See Also
Categories
Find more on Histograms 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!