Rendering a Graph with Annotations
In order to render a graph with annotations, you must include at least one ANNOTATE
statement in the graph template. The ANNOTATE statement causes the annotations to
be read from the SG annotation data set and drawn on the graph. You can place an
ANNOTATE statement anywhere in the template. However, the drawing spaces that are
specified for the annotations must be valid in the context of the ANNOTATE statement
location. The order in which the annotations are drawn with respect to the graph
elements depends on the LAYER option for the annotations. By default, annotations are
always drawn on the front layer. In that case, the annotations are drawn after the graph
elements are drawn and appear on top of the plots and other graph elements. If you
specify BACK for the annotation layer, the annotations are drawn before the graph
elements are drawn and appear behind the plots and other graph elements.
When you render your graph, you must include the SGANNO= option in the
SGRENDER statement to specify the name of your SG annotation data set.For
examples, see “Example 1: Creating Custom Labels” on page 405 and “Example 2:
Displaying Subsets of Annotations in Axis-Aligned Insets” on page 409.
Subsetting Annotations
By default, the ANNOTATE statement draws all of the annotations that are stored in the
SG annotation data set. You can use the ANNOTATE statement ID= option to display a
subset of these annotations. Subsetting your annotations enables you to use one SG
annotation data set with multiple plots. To use the ID= option, the observations for the
annotations must contain an ID column. The ID column is a character column that stores
a unique character value that identifies the subset to which each annotation belongs.
When the ID="identifier" option is used in the ANNOTATE statement, only the
annotations with ID column values that match the identifier value are displayed. The
remaining annotations are ignored. For more information about the ANNOTATE
statement ID= option, see “ANNOTATE Statement” in SAS Graph Template Language:
Reference. For an example, see “Example 2: Displaying Subsets of Annotations in Axis-
Aligned Insets” on page 409.
Examples
Example 1: Creating Custom Labels
Overview
This example demonstrates how to use the Graphic Template Language (GTL)
annotation facility to create custom category labels for a horizontal bar chart. The bar
chart plots the average highway mileage (response) by vehicle type (category).
Ordinarily, the category labels for a horizontal bar chart appear on the Y axis to the left
Example 1: Creating Custom Labels 405
of each bar. This example demonstrates how to print the labels on the left end of each
bar instead. This example also uses the sheen data skin on the bars. Because of the
reflection on the sheen data skin, the labels are raised slightly to center the label in the
reflection on each bar. Finally, the label text color is set to the graph contrast color.
The following figure shows the final graph.
Program
/* Summarize the highway mileage data in SASHELP.CARS. */
proc summary data=sashelp.cars nway;
class type;
var mpg_highway;
output out=mileage mean(mpg_highway) = mpg_highway;
run;
/* Create the annotation data set. */
data anno;
retain function "text" drawspace "datavalue"
textfont "Arial" textweight "bold"
textcolor "GraphData1:contrastColor"
width 100 widthunit "pixel"
anchor "left" x1 2
discreteoffset 0.1;
set mileage(keep=type);
rename type=yc1;
length label $12;
label=type;
run;
/* Create the template. */
proc template;
define statgraph barchart;
begingraph;
entrytitle "Average Highway Mileage by Vehicle Type";
layout overlay /
yaxisopts=(display=(label))
xaxisopts=(label="Average Highway MPG" offsetmax=0.05);
406 Chapter 19 Adding Data-Driven Annotations to Your Graph

Get SAS 9.4 Graph Template Language, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.