Creating Heatmaps using GTL

2

The heatmap is a graphical representation of a table where colors are used to represent the values in the table. This is an effective graphic for finding the minimum and maximum values across the table and may surface patterns in the data.

With the addition of the HEATMAPPARM statement to the GTL syntax in 9.3, heatmaps can be created with just a few lines of code as shown below:

 
proc template;
  define statgraph heatmap;
    begingraph;
      entrytitle 'How often do you read these materials because you want to?';
        layout overlay /* see complete code listing for details */
	  heatmapparm x=frequency y=type colorresponse=percent /  name="heatmap";
	  scatterplot x=frequency y=type / markercharacter=percent
                                           markercharacterattrs=(size=10);
	  continuouslegend "heatmap" / valueattrs=(size=8);
	endlayout;
    endgraph;
  end;
run;
 
proc sgrender data=reading template=heatmap;
run;

Complete SAS program

In the example above, the COLORRESPONSE option specifies that the colors are computed for the crossings using a linear mapping of the values to a color gradient. The actual values for each crossing are shown by overlaying a SCATTERPLOT statement and using its MARKERCHARACTER option.

The X= and Y= options on the HEATMAPPARM statement support either character or numeric data. If numeric data is provided the bins are assumed to be at equal intervals. Users can specify variable sized bins by using the XBOUNDARIES or YBOUNDARIES options. Note that the HEATMAPPARM statement assumes that the data is already aggregated for each crossing.

In the example below, the different age groups are specified by listing the start values for each group in the XBOUNDARIES option. The color scale has been bound to 0 at the lower end to provide an equitable mapping of color in this context.

proc template;  
  define statgraph heatmap;
    begingraph;
      entrytitle 'Unemployment in the USA by Ethnicity in Q2, 2011';
      rangeattrmap name="rmap";
        range 0 - max / rangecolormodel=(white darkblue);
      endrangeattrmap;
      rangeattrvar attrmap="rmap" var=unemployment_rate attrvar=pColor;
      layout overlay / yaxisopts=(display=(ticks tickvalues line));
        heatmapparm x=Age y=Ethnicity colorresponse=pColor /
                          xboundaries=(16 20 25 35 55 65)
                          xvalues=leftpoints xendlabels=true
                          name="heatmap";
	continuouslegend "heatmap";
      endlayout;
    endgraph;
  end;
run;
 
proc sgrender data=unemployment template=heatmap;
run;

Complete SAS program

Share

About Author

Pratik Phadke

Software Developer

Pratik Phadke is a Senior Developer in the Data Visualization group at SAS Institute. He has worked on interactive visualization components used in various SAS products, including Enterprise Miner and Forecast Studio as well as ODS Graphics. He received a master's degree in Computer Science from the University of Maryland, Baltimore County.

Related Posts

2 Comments

  1. Hi Pratik,
    How can I make a color block a square? In your example, color blocks are oblong shape.

    Thanks

Back to Top