Good evening,
I'm wondering if there is a way to plot different histograms on the same graph exploiting grouping variables. I know the function boxplot does it on its own, but I'm, wondering if there is a way to do it using histogram(.).
Basically I want to plot the variation for consumption for different age grups/income quintiles/etc which I have stored as categorical variables.
Can anyone help?
Thank you very much in advance!
Lorenzo

댓글 수: 1

dpb
dpb 2016년 12월 29일
W R2014b histcounts and histogram support categorical variables to at least some degree...w/o knowing precisely what your data are and what you want, whether directly out of the box solution or not, can't tell...

댓글을 달려면 로그인하십시오.

 채택된 답변

the cyclist
the cyclist 2016년 12월 29일
편집: the cyclist 2016년 12월 29일

2 개 추천

I don't believe there is a built-in grouping variable capability in histogram, but it is pretty easy to code

% Made-up data
N = 1000;
x = randn(N,1);
% Grouping variable will be either 0 or 1
gv = round(rand(N,1));
% Offset the gv=1 data, so the histograms are more distinct
x(gv==1) = x(gv==1) + 3;
% Plot the two histograms
figure
hold on
histogram(x(gv==0))
histogram(x(gv==1))
legend({'gv=0','gv=1'})

댓글 수: 3

Lorenzo Cito
Lorenzo Cito 2016년 12월 29일
Thank you very much, I had tought of something similar. Do you think is it possible to make it work with "gv" being a cell array cointaining words rather than 1 and 0s?
Probably I'm coding something wrong as I don't think histogram(x(gv=='Old')) - for example - is the proper way to write it.
Thank you for your patience, I'm quite new with MatLab and programming in general!
There are many possible ways to do get the logical array you need from the cell. Here is one:
C = {'Old','New','New'}; % Your grouping
ismember(C,'Old'); % Logical variable pointing to only first one
Lorenzo Cito
Lorenzo Cito 2016년 12월 30일
Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2016년 12월 29일

댓글:

2016년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by