How to have a numerical legend showing with a categorical histogram?

6 ビュー (過去 30 日間)
Konstantin J
Konstantin J 2022 年 5 月 10 日
編集済み: dpb 2022 年 5 月 11 日
Hi,
I am trying to do something like in the following example below. I am trying to get a histogram with the distribution of genders. But additionally, I would like to include another array with stroke values (0 - no stroke, 1 - stroke). I want them to be in legend, but also as bars.
When I do this, I get:
Any help is appreciated,
  1 件のコメント
dpb
dpb 2022 年 5 月 10 日
編集済み: dpb 2022 年 5 月 10 日
You need to use the 'grouped' style with the y data a 3 row by 2 column array to get the grouped plot. Folks could help a lot easier if you would attach your data file instead of having to make up something...

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2022 年 5 月 10 日
The legend labels are just text; you can make then whatever you want -- try something like:
nStroke=[2000,100;2750,125]; % make up roughly same data
gender=categorical({'Male','Female'},{'Male','Female'},"Ordinal",1);
hB=bar(gender,nStroke,'grouped','BarWidth',1);
ylim([0 2900])
hLg=legend('0','1','location','northwest');
hLg.Title.String='Stroke';
pretty-much reproduces your example figure. "Salt to suit!" for colors, other details...
  4 件のコメント
Konstantin J
Konstantin J 2022 年 5 月 11 日
There is one column with observed values of "1" if a person has had a stroke in the past and 0 if there was no stroke. Do you mean that?
dpb
dpb 2022 年 5 月 11 日
編集済み: dpb 2022 年 5 月 11 日
Oh. I see. Looking at one of the models that echo'ed the input I see the "Stroke" column is in the dataset but isn't listed/shown in the preview window.
Attach the .mat file you've loaded here; it takes a registration from that site that I don't have/don't want to create.
But, in general, to get the data to create the plot from the raw data using the observed stroke data,
tStroke=readtable('StrokeFile.csv');
tStroke.gender=categorical(tStroke.gender,{'Male','Female'},"Ordinal",1);
tStroke.stroke=logical(tStroke.stroke);
tG=groupsummary(tStroke,{'gender','stroke'});
will then give you the summary statistics from which the bar graph can be created.
ADDENDUM: The definition of the categorical variable for gender is as it is so the plot will show the x axis order of "Male" first; otherwise MATLAB creates/orders categories alphabetically which would put "Female" first. That's the only reason for making ordinal.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by