在直方图/条形图中绘制两个分类数组? [英] Plotting two categorical arrays in a histogram/bar chart?
问题描述
我有一个分类数组,种族和一个是/否数组,我想以某种方式创建一个堆叠的条形图/直方图,每个种族都有自己的条形图,每个条形图分成两种不同的颜色-一个回答是"的受访者,其他回答否"的受访者.有没有办法在MATLAB中相对简单地做到这一点?至少有一种方法可以创建一张表格来显示每个种族,有多少人说是,有多少人说不?
I have a categorical array, race, and an array of yes/no, and I want to somehow create a stacked bar/histogram plot with each race having its own bar and each bar is broken up into two different colors - one for the respondents that said yes, and the others for the ones that said no. Is there any way to do this relatively simply in MATLAB? And is there a way at least create a table that shows for each race, how many said yes, how many said no?
为澄清起见,我的数据集中有1250行,每一行代表一个人的回答.我将其拆分,因此有一个名为YESNO
的标称数组,其标称值为1250x1.它具有Y
或N
.我也有一个称为RACE
的标称数组,它是1250x1,并选择了5种不同的种族.
To clarify, there are 1250 rows in my data set, each row representing the responses of a person. I split it up so there is a nominal array called YESNO
that is 1250x1 nominal. It has Y
or N
. I also have a nominal array called RACE
, which is 1250x1, and has 5 different races chosen.
我想以某种方式制作一个像这样的直方图,该直方图是我在Tableau中制作的(是"是橙色,蓝色是否"):
I would like to somehow make a histogram that looks like this, which I made in Tableau ("yes" is orange, blue is "no"):
如果我做不到这一点,那么我至少希望有一个表格可以显示每个种族,然后显示多少人回答是",多少人回答否".
If I can't do this, I would at least like to be able to have a table that shows each race and then how many responded with "yes" and how many with "no".
我尝试执行以下操作:
bar(RACE,YESNO)
我发现XData值必须唯一.
And I get that the XData values must be unique.
因此,我然后尝试对数据进行直方图:
So then I tried doing a histogram of the data:
histogram(RACE,YESNO)
我得到的东西看起来像这样:
And I get something that looks like this:
这根本不是我想要的.我一直在寻找整个文档,以查看是否有一种方法可以使用此分类数据或至少是堆叠的直方图进行堆叠的条形图,但是似乎没有任何方法可以向我指出正确的方向.对于分类数据的堆叠直方图,我找不到任何内容,并且bar
不允许我使用当前数据进行建模.
Which is not at all what I want. I've been looking all over the documentation to see if there's a way to do a stacked bar graph with this categorical data, or at least a stacked histogram, but nothing seems to be able to point me in the right direction. I can't find anything on doing a stacked histogram of categorical data, and bar
is not allowing me to use my current data to be modelled.
此外,我也愿意使用医院数据集以查看示例.可以通过类似的方式绘制性别栏来反对吸烟.
Also, I would be willing to use the hospital data set included in MATLAB to see an example. There is the gender column that can be plotted against smoking in a similar manner.
在MATLAB中是否有解决此问题的方法?
Is there a way of going about this in MATLAB?
推荐答案
假设您的数据如下所示:
Assuming your data looks like this:
yesno = categorical(randi(2,1250,1),[1 2],{'no','yes'});
race = categorical(randi(5,1250,1),1:5,{'Asian','Black','BHispanic','White','WHispanic'});
您可以执行以下操作:
% convert everything to numeric:
yn = double(yesno);
rac = double(race);
% caluculate all frequencies:
data = accumarray([rac yn],1);
% get the categories names:
races = categories(race);
answers = categories(yesno);
% plotting:
bar(data,0.4,'stacked');
ax = gca;
ax.XTickLabel = races; % set the x-axis ticks to the race names
legend(answers) % add a legend for the colors
colormap(lines(3)) % use nicer colors (close to your example)
ylabel('YES/NO')% set the y-axis lable
% some other minor fixes:
box off
ax.YGrid = 'on';
结果:
您可以使用以下方法制作一张桌子:
And you can make a table from it with:
T = array2table(data.','VariableNames',races,'RowNames',answers)
输出:
T =
Asian Black BHispanic White WHispanic
_____ _____ _________ _____ _________
no 126 123 102 128 144
yes 145 126 128 105 123
这篇关于在直方图/条形图中绘制两个分类数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!