Examples


The following examples show how to annotate graphics that are created with SAS/ GRAPH procedures and how to build custom graphics:

  • Labeling Cities on a Map on page 604

  • Labeling Subgroups in a Vertical Bar Chart on page 607

  • Drawing a Circle of Stars on page 609

Other examples that use Annotate data sets are as follows :

  • Example 1 on page 710 (and others in that chapter

  • Chapter 44, The GSLIDE Procedure, on page 1277

  • Drawing a Circle of Stars on page 609

Labeling Cities on a Map

Features:

Annotate

LABELSYMBOL

function:

 

Annotate

HSYS

variables :

 
 

POSITION

 

SIZE

 

TEXT

 

WHEN

 

X and Y

 

XSYS

 

YSYS

Sample library member:

GANCITY

click to expand
Figure 24.9: Map with Labeled Cities

This example labels a map of the continental United States with the location and names of three cities. The GMAP procedure draws a map of the U.S. and an Annotate data set adds the stars and labels.

The DATA step that creates the Annotate data set gets the x and y coordinates of the cities to be labeled from the MAPS.USCITY data set. Because MAPS.USCITY stores projected coordinates in the X and Y variables, the DATA step does not need to reassign the variable values. Also because X and Y contain data values (the map data set coordinates), the XSYS and YSYS variables specify coordinate system 2, absolute data values. However, the HSYS variable that controls text height uses coordinate system 3, percent of the graphics output area.

See Example 4 on page 1180 for an example of labeling a map using map coordinates in units of latitude and longitude.

See Chapter 35, The GMAP Procedure, on page 995 for more information on using map data sets.

Note: If the libref MAPS is automatically assigned at your site to the SAS data library containing the Institute-supplied map data sets, you can omit the LIBNAME statement.

Assign the libref MAPS, if necessary, and set the graphics environment.

 libname maps '  SAS-data-library  ';  goptions reset=global gunit=pct border cback=white           colors=(black blue green red)           ftext=swissb htitle=6 htext=3; 

Subset the U.S. map data set by omitting Alaska and Hawaii.

 data lower48;     set maps.us;     if state ne stfips('AK');     if state ne stfips('HI');  run; 

Create the Annotate data set, CITYSTAR. CITYSTAR contains the commands that draw a star and a label at each of the three cities. Setting WHEN to A draws the annotation after the map.

 data citystar;     length function style color $ 8 position $ 1            text $ 20;     retain xsys ysys '2' hsys '3'            when 'a'; 

Include the values of selected variables from MAPS.USCITY. X and Y contain projected coordinates; CITY contains names; STATE contains FIPS codes. Because there are several Atlantas, a STATE value is necessary.

 set maps.uscity(keep=x y city state);  if (city='Atlanta' and state=13)      or city='Chicago'      or city='Seattle'; 
  • Create the observation that draws the star. The text string V is the character code for the star figure in the MARKER font assigned by the STYLE variable.

     function='symbol'; style='marker'; text='V'; color='red'; size=5;     output; 
  • Create the observation that labels the city. TEXT is assigned the value of CITY. The font is SWISSB. SIZE uses the units assigned by HSYS so text height is 5 percent of the height of the graphics output area. POSITION 8 places the label directly below the city location.

     function='label'; style='swissb'; text=city; color='green';        size=5; position='8'; output;  run; 
  • Define the title and footnote for the map.

     title 'Distribution Center Locations';  footnote font=swiss j=r 'GANCITY'; 

Define patterns for the map areas. MEMPTY colors only the state borders.

 pattern value=mempty color=blue repeat=49; 

Generate the map and assign the annotate data set to the CHORO statement.

 proc gmap data=lower48 map=lower48;     id state;     choro state / annotate=citystar discrete nolegend;  run;  quit; 

Labeling Subgroups in a Vertical Bar Chart

Features:

Annotate function:

LABEL (default)

Annotate variables:

MIDPOINT

 

POSITION

 

SUBGROUP

Sample library member:

GANVBAR

click to expand
Figure 24.10: Bar Chart with Labeled Subgroups

This example shows how to label subgroups in a vertical bar chart that is generated by the GCHART procedure. Each bar represents total orders for a city and is subgrouped by the type of order. The Annotate facility labels each subgroup with the number of orders for that category. The coordinates that position the subgroup labels are derived from the values of the GCHART procedure variables CITY (the chart (or midpoint) variable) and TYPE (the subgroup variable). These variables are assigned to the corresponding Annotate variable.

See Chapter 29, The GCHART Procedure, on page 773 for more information on creating bar charts .

Assign the libref and set the graphics environment.

 libname reflib '  SAS-data-library  ';  goptions reset=global gunit=pct border cback=white           colors=(blue green red) ctext=black htitle=6           ftitle=swissb htext=4 ftext=swiss; 

Create the data set SOLD.

 data sold;     length type $ 10;     input city $ units type $ ;     datalines;  Atlanta  99 Printers  Atlanta 105 Plotters  Atlanta  85 Terminals  Paris   182 Printers  Paris   150 Plotters  Paris   157 Terminals  Sydney  111 Printers  Sydney  136 Plotters  Sydney  100 Terminals  ;  run; 

Create the Annotate data set, BARLABEL. The MIDPOINT variable uses the values of the chart variable CITY to provide the X coordinate for the subgroup labels. The SUBGROUP variable uses the values of the variable TYPE to provide the Y coordinate that vertically positions the labels in the bar. Because no function is specified, the data set uses the default function, LABEL. The POSITION value E places the labels just below the top of each subgroup bar.

 data barlabel;     length color style $ 8;     retain color 'white' when 'a' style 'swissb'        xsys ysys '2' position 'E' size 4 hsys '3';     set sold;     midpoint=city;     subgroup=type;     text=left(put(units,5.));  run; 

Define the title and footnote.

 title 'Orders Received';  footnote h=3 j=r 'GANVBAR'; 

Define axis characteristics. AXIS1 suppresses the vertical axis. AXIS2 drops the midpoint axis label.

 axis1 label=none major=none minor=none style=0        value=none;  axis2 label=none; 

Generate a vertical bar chart and assign the Annotate data set to the VBAR statement.

 proc gchart data=sold;     vbar city / type=sum                 sumvar=units                 subgroup=type                 width=17                 raxis=axis1                 maxis=axis2                 annotate=barlabel;  run;  quit; 

Drawing a Circle of Stars

Features:

Annotate function:

BAR

 

CNTL2TXT

 

FRAME

 

LABEL

 

MOVE

 

PIECNTR

 

PIEXY

 

SYMBOL

Annotate variables:

COLOR

 

HSYS, XSYS, YSYS

 

LINE

 

STYLE

 

TEXT

 

X and YXLAST and YLAST

 

XLSTT and YLSTT

Sample library member:

GANCIRCL

click to expand
Figure 24.11: Stars Positioned in a Circle with GANNO

This example shows how to use an Annotate data set to draw a flag that is composed of a rectangle and four stars. The stars are positioned by placing them on an imaginary circle. The program uses the PIECNTR and PIEXY functions to find the points on the circle and the CNTL2TXT programming function to transfer coordinate values. It also processes Annotate assignment statements in a DO loop. The GANNO procedure displays the Annotate graphics.

Set the graphics environment.

 goptions reset=global cback=white colors=(black); 

Create the Annotate data set, FLAG. XSYS, YSYS, and HSYS specify coordinate system 3, absolute size of the graphics output area.

 data flag;     length function style color $ 8 text $ 30;     retain xsys ysys hsys '3'; 

Draw a frame. The FRAME function uses the default color BLACK to draw a frame around the graphics output area specified by the XSYS and YSYS variables.

 function='frame'; output; 

Draw the footnote. The LABEL function draws the text specified in the TEXT variable. X and Y explicitly position the footnote on the graphics output area.

 function='label'; x=92; y=5; text='GANCIRCL';     style='swiss'; size=3; position='5'; output; 

Draw the title. The values of FUNCTION, POSITION, and COLOR remain the same because no new values are assigned.

 x=50; y=90; text='Flag of Micronesia';     style='swissb'; size=6; output; 

Draw the background. MOVE specifies the lower left corner of the rectangle that forms the flag. BAR draws the rectangle using the values of X and Y for the upper right corner. The LINE value of 3 fills the figure with the specified color.

 function='move'; x=20; y=30; output;  function='bar'; x=80; y=80; color='blue';     line=3; style='solid'; output; 

Draw the circle of stars. The DO loop repeats the processing instructions defined by the nested assignment statements, placing a star every 90 degrees around the circle. To increase the number of stars, reduce the size of the angle between them and adjust the ending angle.

 do star_ang=0 to 270 by 90; 

The PIECNTR function is set to the center of the rectangle. PIEXY calculates a point on the arc based on the value of STAR_ANG and updates the internal coordinates XLAST and YLAST.

 function='piecntr'; x=50; y=55; size=15; output;  function='piexy'; size=1; angle=star_ang; output; 

The programming function CNTL2TXT copies the values of XLAST and YLAST to the text-handling coordinates XLSTT and YLSTT. Assigning missing values to X and Y forces the SYMBOL function to use the values of XLSTT and YLSTT to position the star. The text string V is the character code for the star figure in the MARKER font assigned by the STYLE variable.

 function='cntl2txt'; output;     function='symbol'; style='marker'; text='V';        angle=0; color='white'; size=10; x=.; y=.;        output;     end;  run; 

Use the GANNO procedure to process the Annotate data set and generate the graphics output.

 proc ganno annotate=flag;  run;  quit; 



SAS.GRAPH 9.1 Reference, Volumes I and II
SAS.GRAPH 9.1 Reference, Volumes I and II
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 342

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net