Data visualization with SAS programming

Compare two variables distribution using same axis

Reply
Frequent Contributor
Posts: 99

Compare two variables distribution using same axis

Hi all,

           I have 2 variables, which capture similiar information but their distributions are different. I want to plot their distribution on same axis. The distribution could be a histogram.

          Thanks for help.

Amit

    output wanted:

Output.png

Sample Data:

IndexVar1Var2
10.74691.025641
20.37970.474684
30.46030.854430
40.56700.974729
50.51900.622837
60.59410.693069
70.43480.869565
80.65481.014085
90.31750.476190
100.41670.555556
SAS Super FREQ
Posts: 1,148

Re: Compare two variables distribution using same axis

TwoDist.png


data TwoVar;
  input Index Var1 Var2;
  datalines;
1  0.7469  1.025641
2  0.3797  0.474684
3  0.4603  0.854430
4  0.5670  0.974729
5  0.5190  0.622837
6  0.5941  0.693069
7  0.4348  0.869565
8  0.6548  1.014085
9  0.3175  0.476190
10 0.4167  0.555556
;
run;

ods graphics / reset width=5in height=3in imagename='TwoDist';

title 'Comparative Density';

proc sgplot data=TwoVar;

  density var1 / legendlabel='Var1';

  density var2  / legendlabel='Var2';

  keylegend / across=1 location=inside position=topright;

  xaxis display=(nolabel);

  run;


Frequent Contributor
Posts: 99

Re: Compare two variables distribution using same axis

Hi Sanjay,

                 Thanks for your reply. I have  a follow up question.

                     Which option made the two density on same axis?

                    Also could you do a histogram on same axis?

Regards,

Amit

SAS Super FREQ
Posts: 1,148

Re: Compare two variables distribution using same axis

The axis are a union of the data automatically.  You don't have to specify anything extra. Smiley Happy

Yes, you can have histograms.  Just replace the "density" statements with the "histogram" statements.

You can have histograms AND density plots at the same time.  You just have to a bit more to control the transparencies, colors and legends.

See below.

TwoDistHist.png

title 'Comparative Density';

proc sgplot data=TwoVar nocycleattrs;

  histogram var1 / legendlabel='Var1' fillattrs=graphdata1  transparency=0.3;

  density var1  / legendlabel='Var1'  lineattrs=graphdata1(pattern=solid) name='1';

  histogram var2 / legendlabel='Var2' fillattrs=graphdata2  transparency=0.3;

  density var2  / legendlabel='Var2' lineattrs=graphdata2(pattern=solid) name='2';

  keylegend '1' '2' / across=1 location=inside position=topright;

  xaxis display=(nolabel);

  run;

Respected Advisor
Posts: 3,156

Re: Compare two variables distribution using same axis

Hi Sanjay@SAS,

Great demonstration! I am wondering if this can be easily done using EG? I am new to EG and have played with it aiming at this target with no avail.

Thanks in advance!

Haikuo

SAS Super FREQ
Posts: 1,148

Re: Compare two variables distribution using same axis

Yes, you can write proc SGPLOT code programs in EG.

Respected Advisor
Posts: 3,156

Re: Compare two variables distribution using same axis

Hi Sanjay,

Sorry I did not make myself clear at the first place. I am curious if the same can be done using menu-driven method in EG. If possible, how?

Thanks,

Haikuo

SAS Super FREQ
Posts: 1,148

Re: Compare two variables distribution using same axis

May be easier if you post the question to the EG track.

SAS Super FREQ
Posts: 3,637

Re: Compare two variables distribution using same axis

For additional options, see Overlay density estimates on a plot - The DO Loop

Also, notice that the default SGPLOT behavior is to use separate bin widths for each variable, which is often not what you want. In 9.3, you can control this behavior from SGPLOT. If you are still using 9.2, see this post to set the bin widths to be equal: Overlaying two histograms in SAS - The DO Loop

Ask a Question
Discussion stats
  • 8 replies
  • 1446 views
  • 0 likes
  • 4 in conversation