A reader commented to me that he wants to use the HISTOGRAM statement of the SGPLOT procedure to overlay two histograms on a single plot. He could do it, but unfortunately SAS was choosing a large bin width for one of the variables and a small bin width for the other variable. "The figure looks odd because the bin widths vary so much," he wrote, "so I would like to [set]the width."
He asked me whether it is possible to control the bin width of a histogram from the HISTOGRAM statement. The answer is "not in SAS 9.2, but stay tuned for SAS 9.3!"
So what can you do in SAS 9.2? You can control the histogram bin width by using the Graph Template Language (GTL).
Defining a Template That Overlays Two Histograms
To illustrate this approach, I'll overlay histograms of the SEPALLENGTH and PETALLENGTH variables in the SASHELP.IRIS data set. Most of the statements for the following template are explained in the getting started example in the GTL documentation:
proc template; define statgraph dualhist; begingraph; entrytitle "Petal and Sepal Lengths"; /** optional title **/ layout overlay / xaxisopts=(label="Length"); /** first plot: a histogram **/ histogram PetalLength / name="PetalLength" binwidth=5; /** second plot: a semi-transparent histogram **/ histogram SepalLength / name="SepalLength" binwidth=5 datatransparency=0.7 fillattrs=(color=GraphData2:color); /** optional: add legend by specifying names **/ discretelegend "PetalLength" "SepalLength"; endlayout; endgraph; end; run;
For this particular template:
- The LAYOUT OVERLAY statement specifies that the graph consists of two plots, one on top of the other, and a legend.
- The first HISTOGRAM statement specifies that the first plot is a histogram of the PETALLENGTH variable. The BINWIDTH= option specifies that the histogram should use a bin width of 5.
- The second HISTOGRAM statement specifies that the second plot is a histogram of the SEPALLENGTH variable. Again, the histogram should use a bin width of 5. Furthermore, the second histogram should have semi-transparent bars that are filled with a different color. Which color? The second color in a pre-defined list of colors.
- The DISCRETELEGEND statement adds a legend that associates the colors to the variables.
The RUN statement results in the template being compiled and stored in an output template named DUALHIST. The template is stored in the default template folder, but no graph is produced at this time.
In order to create (or "render") the graph, you need to call the SGRENDER procedure. You must provide PROC SGRENDER with the name of the data set and the name of the template, as follows:
proc sgrender data=sashelp.iris template=dualhist; run;
As shown in the image, the second histogram (pink color) is overlaid on the first. Because the second histogram is semi-transparent, you can see the first histogram underneath. Furthermore, where the two histograms intersect, the color is purple, which is an additive mixture of the blue and pink colors.
You can learn more about the Graph Template Language if you decide to write your own templates. I also recommend the book Statistical Graphics in SAS: An Introduction to the Graph Template Language and the Statistical Graphics Procedures by my colleague, Warren Kuhfeld.