SGPLOT Procedure

Example 8: Combining Histograms with Density Plots

Features:

HISTOGRAM statement

DENSITY statement

KEYLEGEND statement

Sample library member: SGPLHST
This example shows a histogram combined with two density plots. One density plot uses a normal density estimate and the other density plot uses a kernel density estimate.

Output

SGPLHST - Combining Histograms with Density Plots

Program

proc sgplot data=sashelp.heart;
  title "Cholesterol Distribution";
  histogram cholesterol;
  density cholesterol;
  density cholesterol / type=kernel;
  keylegend / location=inside position=topright;
run;
title;

Program Description

Set the title, set a label for the X axis, and create the histogram.
proc sgplot data=sashelp.heart;
  title "Cholesterol Distribution";
  histogram cholesterol;
Create the density plots. The TYPE= option specifies which density equation is used.
  density cholesterol;
  density cholesterol / type=kernel;
Position the Legend The LOCATION= option places the legend inside of the plot area. The POSITION= option places the legend at the top right.
  keylegend / location=inside position=topright;
run;
Cancel the title.
title;