Plotting two categorical arrays in a histogram/bar chart?

半城伤御伤魂 提交于 2019-12-08 00:27:17

问题


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?

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.

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".

I've tried to do the following:

bar(RACE,YESNO)

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:

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.

Is there a way of going about this in MATLAB?


回答1:


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'});

You can do the following:

% 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';

The result:

And you can make a table from it with:

T = array2table(data.','VariableNames',races,'RowNames',answers)

The output:

T = 
           Asian    Black    BHispanic    White    WHispanic
           _____    _____    _________    _____    _________
    no     126      123      102          128      144      
    yes    145      126      128          105      123  


来源:https://stackoverflow.com/questions/42312725/plotting-two-categorical-arrays-in-a-histogram-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!