Chapter Contents

Previous

Next
SAS/GRAPH Software: Reference

LEGEND Statement

The LEGEND statement controls the location and appearance of legends on two-dimensional plots, contour plots, maps, and charts.

Used by:
GCHART, GCONTOUR, GMAP, and GPLOT procedures

Global

Description

LEGEND statements specify the characteristics of a legend but do not create legends. These characteristics are

LEGEND definitions are not automatically applied when a procedure generates a legend. Instead, they must be explicitly assigned with a LEGEND= option in the appropriate procedure statement.

illustrates the terms associated with the various parts of a legend.

Parts of a Legend

[IMAGE]


Syntax

LEGEND<1...99><options>;

option(s) can be one or more options from any or all of the following categories:


Options

When the syntax of an option includes units, use one of these:
CELLS character cells
CM centimeters
IN inches
PT points
PCT percentage of the graphics output area

If you omit units, a unit specification is searched for in this order:

  1. GUNIT= in a GOPTIONS statement

  2. the default unit, CELLS.

ACROSS=number-of-columns
specifies the number of columns to use for legend entries.
Featured in: Example 8. Creating a Simple Web Page with the ODS HTML Statement

CBLOCK=block-color
generates and colors a three-dimensional block effect behind the legend. The size and position of the block are controlled by the graphics option OFFSHADOW=(x,y).

CBLOCK= and CSHADOW= are mutually exclusive. If both are present, SAS/GRAPH software uses the last one specified. CBLOCK= is usually used in conjunction with the FRAME, CFRAME=, or CBORDER= options.
See also: Creating Drop Shadows and Block Effects and the OFFSHADOW= graphics option

CBORDER=frame-color
draws a colored frame around the legend. This option overrides the FRAME option. CBORDER= can be used in conjunction with the CFRAME= option.

CFRAME=background-color
specifies the background color of the legend. This option overrides the FRAME option. If both CFRAME= and FRAME= are specified, only the solid background produced by CFRAME= is displayed. CFRAME= can be used in conjunction with the CBORDER= option.

CSHADOW=shadow-color
generates and colors a drop shadow behind the legend. The size and position of the shadow is controlled by the graphics option OFFSHADOW=(x,y).

CSHADOW= and CBLOCK= are mutually exclusive. If both are present, SAS/GRAPH uses the last one specified. CSHADOW= is usually specified in conjunction with the FRAME, CFRAME=, or CBORDER= options.
See also: Creating Drop Shadows and Block Effects and the OFFSHADOW= graphics option

DOWN=number-of-rows
specifies the number of rows to use for legend entries.

FRAME
draws a frame around the legend. The color of the frame is the first color in the colors list.

FWIDTH=thickness-factor
specifies the thickness of the frame, where thickness-factor is a number. The thickness of the line increases directly with thickness-factor. By default, FWIDTH=1.

LABEL=(text-argument(s)) | NONE
modifies a legend label. Text-argument(s) defines the appearance or the text of a legend label, or both. NONE suppresses the legend label. By default, the text of the legend label is either the variable name or a previously assigned variable label. Text-argument(s) can be one or more of these:

`text-string'
provides up to 256 characters of label text. Enclose each string in quotes. Separate multiple strings with blanks.

text-description-suboption
modifies a characteristic such as the font, color, or size of the text string(s) that follows it. Text-description-suboption can be
COLOR=text-color
FONT=font | NONE
HEIGHT=text-height <units>
JUSTIFY=LEFT | CENTER | RIGHT
POSITION=(<BOTTOM | MIDDLE | TOP> <LEFT | CENTER | RIGHT>)

See Text Description Suboptions for complete descriptions.

Specify as many text strings and text description suboptions as you want, but enclose them all in one set of parentheses.
Featured in: Example 3. Rotating Plot Symbols through the Colors List and Example 8. Creating a Simple Web Page with the ODS HTML Statement

MODE=PROTECT | RESERVE | SHARE
specifies whether or not the legend is drawn in the procedure output area or whether legend elements can overlay other graphics elements. MODE= can take one of these values:
PROTECT draws the legend in the procedure output area, but a blanking area surrounds the legend, preventing other graphics elements from being displayed in the legend. (A blanking area is a protected area in which no other graphics elements are displayed.)
RESERVE takes space for the legend from the procedure output area, thereby reducing the amount of space available for the graph. If MODE=RESERVE is specified in conjunction with OFFSET=, the legend may push the graph off the graphics output area. RESERVE is valid only when POSITION=OUTSIDE. If POSITION=INSIDE is specified, a warning is issued and MODE= is changed to PROTECT.
SHARE draws the legend in the procedure output area. If the legend is positioned over elements of the graph itself, both graphics elements and legend elements are displayed.

By default, MODE=RESERVE unless POSITION=INSIDE, in which case the default changes to MODE=PROTECT.
See also: Positioning the Legend
Featured in: Example 8. Creating a Simple Web Page with the ODS HTML Statement

OFFSET=(<x><,y>)<units> | (<x <units>><,y <units>>)
specifies the distance to move the entire legend; x is the number of units to move the legend right (positive numbers) or left (negative numbers), and y is the number of units to move the legend up (positive numbers) or down (negative numbers).

To set only the x offset, specify one value, with or without a following comma:

offset=(4 cm,)

To set both the x and y offset, specify two values, with or without a comma separating them:

offset=(2 pct, 4 pct)

To set only the y offset, specify one value preceded by a comma:

offset=(,-3 pct)

OFFSET= is usually used in conjunction with POSITION= to adjust the position of the legend. Moves are relative to the location specified by POSITION=, with OFFSET=(0,0) representing the initial position. You can also apply OFFSET= to the default legend position.

OFFSET= is unnecessary with ORIGIN= since ORIGIN= explicitly positions the legend and requires no further adjustment. However, if you specify both options, the values of OFFSET= are added to the values of ORIGIN=, and the LEGEND is positioned accordingly.
See also: Positioning the Legend and the option POSITION=

ORDER=(value-list)
selects or orders the legend values that appear in the legend. The way you specify value-list depends on the type of variable that generates the legend:

For a complete description of value-list, see the ORDER= option in the AXIS statement.

Even though ORDER= controls whether a legend value is displayed and where it appears, the VALUE= option controls the text that the legend value displays.

ORIGIN=(<x><,y>)<units> | (<x <units >><,y <units>>)
specifies the x coordinate and the y coordinate of the lower-left corner of the legend box. ORIGIN= explicitly positions the legend anywhere on the graphics output area. It is possible to run a legend off the page or overlay the graph.

To set only the x coordinate, specify one value, with or without a following comma:

origin=(4 cm,)

To set both the x and y coordinates, specify two values, with or without a comma separating them:

origin=(2 pct, 4 pct)

To set only the y coordinate, specify one value preceded by a comma:

origin=(,3 pct)

ORIGIN= overrides the POSITION= option if both are used. Although using the OFFSET= option with the ORIGIN= option is unnecessary, if OFFSET= is also specified, it is applied after the ORIGIN= request has been processed.
See also: Positioning the Legend
Featured in: Example 8. Creating a Simple Web Page with the ODS HTML Statement

POSITION=(<BOTTOM | MIDDLE | TOP> <LEFT | CENTER | RIGHT> <OUTSIDE | INSIDE>)
positions the legend on the graph. Value for POSITION= are
OUTSIDE or INSIDE specifies the location of the legend in relation to the axis area.
BOTTOM or MIDDLE or TOP specifies the vertical position.
LEFT or CENTER or RIGHT specifies the horizontal position.

By default, POSITION=(BOTTOM CENTER OUTSIDE). You can change one or more settings. If you supply only one value the parentheses are not required. If you specify two or three values and omit the parentheses, SAS/GRAPH accepts the first value and ignores the others.

Once you assign the initial legend position, you can adjust it with the OFFSET= option.

The ORIGIN= options overrides POSITION=. The value of the MODE= option can affect the behavior of POSITION=.
See also: Positioning the Legend and the OFFSET= option and the MODE= option

SHAPE=BAR(width<units>,height<units>) <units> | LINE(length) <units> | SYMBOL(width<units>,height<units>) <units>
specifies the size and shape of the legend values displayed in each legend entry. The value you specify for SHAPE= depends on which procedure generates the legend.

BAR(width,height)<units>
is used with the GCHART and GMAP procedures, the GPLOT procedure if you use the AREAS= option, and the GCONTOUR procedure if you use the PATTERN option. Each legend value is a bar of the specified width and height. By default, width is 5, height is 0.8, and units are CELLS. You can specify units for the width,height pair or for the individual coordinates.
Featured in: Example 3. Rotating Plot Symbols through the Colors List and Example 8. Creating a Simple Web Page with the ODS HTML Statement

LINE(length) <units>
is used with the GPLOT and GCONTOUR procedures. Each legend value is a line of the length you specify. Plotting symbols are omitted from the legend values. By default, length is 5 and units are CELLS. You can specify units for length.

SYMBOL(width<units>,height<units>) <units>
is used with the GPLOT procedure. Each legend value (not each symbol) is the width and height you specify. For example, this specification produces legend values like the ones in Legend Values Produced with SHAPE= SYMBOL(a):
shape=symbol(.5,.5)

This specification produces legend values like the ones in Legend Values Produced with SHAPE= SYMBOL(b):

shape=symbol(2,.5)

Legend Values Produced with SHAPE= SYMBOL

[IMAGE]

By default, width is 5, height is 1, and units are CELLS. You can specify units for the width,height pair or for the individual coordinates.
Featured in: Example 3. Rotating Plot Symbols through the Colors List

VALUE=(text-argument(s) ) | NONE
modifies the legend value descriptions. Text-argument(s) defines the appearance or the text of the value descriptions. By default, value descriptions are the values of the variable that generates the legend or an associated format value. Numeric values are right justified and character values are left justified.

NONE suppresses the value descriptions although the legend values (bars, lines, and so on) are still displayed. Text-argument(s) can be one or more of these:

`text-string'
provides up to 256 characters of text for the value description. Enclose each string in quotes. Separate multiple strings with blanks.

Specified text strings are assigned to the legend values in order. If you submit only one string, only the first legend entry uses the value of that string. If you specify multiple strings, the first string is the text for the first entry; the second string is the text for the second entry; and so forth. For example, this specification produces legend entries like those shown in Specifying Value Descriptions with the VALUE= Option:

value=('1986' '1987' '1988')

Specifying Value Descriptions with the VALUE= Option

[IMAGE]

text-description-suboption
modifies a characteristic such as the font, color, or size of the text string(s) that follows it. Text-description-suboption can be
COLOR=text-color
FONT=font | NONE
HEIGHT=text-height <units >
JUSTIFY=LEFT | CENTER | RIGHT
TICK=n

See Text Description Suboptions for complete descriptions.

Place text description suboptions before the text strings they modify. Suboptions not followed by a text string affect the default values. To specify and describe the text for individual values or to produce multi-line text, use the TICK= suboption.

Specify as many text strings and text description suboptions as you want, but enclose them all in one set of parentheses.

To order or select legend entries, use the ORDER= option.
See also: Text Description Suboptions and the option ORDER=


Text Description Suboptions

Text description suboptions are used by the LABEL= and VALUE= options to change the color, height, justification, font, and angle of either default text or specified text strings. See LABEL= and VALUE=.

COLOR=text-color
C=text-color
specifies the color of the text. If you omit COLOR=, a color specification is searched for in this order:
  1. the CTEXT= option for the procedure

  2. the CTEXT= option in a GOPTIONS statement

  3. the default, the first color in the colors list.

FONT=font | NONE
F=font | NONE
specifies the font for the text. See SAS/GRAPH Fonts for details on specifying font. If you omit FONT=, a font specification is searched for in this order:
  1. the FTEXT= option in a GOPTIONS statement

  2. the default hardware font, NONE.

HEIGHT=text-height <units>
H=text-height <units>
specifies the height of the text characters in the number of units. By default, HEIGHT=1 CELL. If you omit HEIGHT=, a text height specification is searched for in this order:
  1. the HTEXT= option in a GOPTIONS statement

  2. the default value, 1.

JUSTIFY=LEFT | CENTER | RIGHT
J=L | C | R
specifies the alignment of the text. The default for character variables is JUSTIFY=LEFT. The default for numeric variables is JUSTIFY=RIGHT. Associating a character format with a numeric variable does not change the default justification of the variable.

You can use JUSTIFY= to print multiple lines of text by repeating JUSTIFY= before the text string for each line. For example, this statement produces a legend label and value descriptions like those shown in Specifying Multiple Lines of Text with the JUSTIFY= Suboption:

legend label=(justify=c 'Distribution'
              justify=c 'Centers')
value=(tick=1 justify=c 'Portland,'
              justify=c 'Maine'
       tick=2 justify=c 'Paris,'
              justify=c 'France'
       tick=3 justify=c 'Sydney,'
              justify=c 'Australia');

Specifying Multiple Lines of Text with the JUSTIFY= Suboption

[IMAGE]

Specify additional suboptions before any string.
See also: the TICK= suboption

POSITION=(<BOTTOM | MIDDLE | TOP> <LEFT | CENTER | RIGHT>)
places the legend label in relation to the legend entries. The POSITION= suboption is used only with the LABEL= option. By default, POSITION=LEFT.

The parentheses are not required if only one value is supplied. If you specify two or three values and omit the parentheses, SAS/GRAPH accepts the first value and ignores the others.

Using the POSITION= Suboption with Multi-line Legend Labels shows some of the ways POSITION= affects a multi-line legend label in which the entries are stacked in a column (ACROSS=1). This figure uses a label specification such as

label=('multi-'
        justify=left 'line'
        justify=left 'label'
        position=left)

In this specification, POSITION= specifies the default value, LEFT, which is represented by the first legend in the figure. The value of POSITION= is indicated above each legend. The default justification is used unless you also use the JUSTIFY= suboption.

Using the POSITION= Suboption with Multi-line Legend Labels

[IMAGE]

In addition, specifying POSITION=RIGHT mirrors the effect of POSITION=LEFT, and specifying POSITION=BOTTOM mirrors the effect of POSITION=TOP.

TICK=n
T=n
specifies the nth legend entry. The TICK= suboption is used only with the VALUE= option to designate the legend entry whose text and appearance you want to modify. For example, to change the text of the third legend entry to Minneapolis, specify
value=(tick=3 'Minneapolis')

The characteristics of all other value descriptions remain unchanged.

If you use TICK= when you designate text for one legend entry, you must also use it when you designate text for any additional legend entries. For example, this option changes the text of both the second and third legend entries:

value=(tick=2 'Paris' tick=3 'Sydney')

If you omitted TICK=3, the text of the second legend entry would be ParisSydney.

Text description suboptions that precede TICK= affect all the value descriptions for the legend unless the same suboption (with a different value) follows a TICK= specification. Text description suboptions that follow TICK= affect only the specified legend entry. For example, suppose you specify this option for a legend with three entries:

value=(color=red font=swiss tick=2 color=blue)

The text of all three entries would use the Swiss font; the first and third entries would be red and only the second entry would be blue.


Using Text Description Suboptions

Text description suboptions affect all the strings that follow them unless the suboption is changed or turned off. If the value of a suboption is changed, the new value affects all the text strings that follow it. Consider this example:

label=(font=swiss height=4 'Weight'
       justify=right height=3 '(in tons)')

FONT=SWISS applies to both Weight and (in tons). HEIGHT=4 affects Weight, but is respecified as HEIGHT=3 for (in tons). JUSTIFY=RIGHT affects only (in tons).


Using the LEGEND Statement

LEGEND statements can be located anywhere in your SAS program. They are global and remain in effect until canceled or until you end your SAS session. LEGEND statements are not applied automatically, and must be explicitly assigned by an option in the procedure that uses them.

You can define up to 99 different LEGEND statements. If you define two LEGEND statements of the same number, the most recently defined one replaces the previously defined statement of the same number. A LEGEND statement without a number is treated as a LEGEND1 statement.

Cancel individual LEGEND statements by defining a LEGEND statement of the same number without options (a null statement):

legend4;

Canceling one LEGEND statement does not affect any other LEGEND definitions. To cancel all current LEGEND statements, use RESET= in a GOPTIONS statement:

goptions reset=legend;

Specifying RESET=GLOBAL or RESET=ALL cancels all current LEGEND definitions as well as other settings.

To display a list of current LEGEND definitions in the LOG window, use the GOPTIONS procedure with the LEGEND option:

proc goptions legend nolist;
run;


Positioning the Legend

By default, the legend shares the procedure output area with the procedure output, such as a map or bar chart. (See Placement of Graphic Elements in the Graphics Output Area.) However, several LEGEND statement options allow you to position a legend anywhere on the graphics output area and even to overlay the procedure output. This section describes these options and their effect on each other.

Positioning the Legend on the Graphics Output Area

There are two ways you can position the legend on the graphics output area:


Using POSITION= and OFFSET=

The values of the POSITION= option affect the legend in two ways:

Legend Positions Inside the Axis Area shows the legend positions inside the axis area.

Legend Positions Inside the Axis Area

[IMAGE]

Legend Positions Outside the Axis Area shows legend positions outside the axis area.

Legend Positions Outside the Axis Area

[IMAGE]

The default combination is POSITION=(BOTTOM CENTER OUTSIDE). The combination (OUTSIDE MIDDLE CENTER) is not valid.

Use OFFSET=(x,y) to adjust the position of the legend specified by POSITION=. The x value shifts the legend either left or right and the y value shifts the legend either up or down.

The offset values are always applied after the POSITION= request. For example, if POSITION=(TOP RIGHT OUTSIDE), the legend is located in the upper right corner of the graphics output area. If OFFSET=(0,0) is specified, the legend does not move. If OFFSET=(-5,-8)CM, the legend moves 5 centimeters to the left and 8 centimeters down.

Using ORIGIN=

Use ORIGIN=(x,y) to specify the coordinates of the exact location of the lower left corner of the legend box. Because ORIGIN=(0,0) is the lower left corner of the graphics output area, the values of x and y must be positive. If you specify negative values, a warning is issued and the default value is used.

Relating Legends to Other Graphic Elements

By default, the legend is inside the procedure output area and the space it occupies reduces the size of the graph itself. To control the way the legend relates to the other elements of the graph, use the MODE= option. These are values for MODE=:


Interactions Between POSITION= and MODE=

You cannot specify both POSITION=INSIDE and MODE=RESERVE because MODE=RESERVE assumes the legend is outside the axis area, and POSITION=INSIDE positions the legend inside the axis area. Therefore, when you specify POSITION=INSIDE, change the value of MODE= to SHARE or PROTECT. Otherwise, SAS/GRAPH issues a warning and automatically changes the value of MODE= to PROTECT.


Creating Drop Shadows and Block Effects

To produce a drop shadow or a three-dimensional block effect behind the legend use the CSHADOW= or CBLOCK= option in the LEGEND statement in conjunction with the graphics option OFFSHADOW=(x,y).

The value of x determines how far the shadow or block extends to the right (positive numbers) or to the left (negative numbers) of the legend. The value of y determines how far the shadow or block extends above (positive numbers) or below (negative numbers) the legend. If OFFSHADOW=(0,0) is specified, the shadow or block is not visible.

By default, OFFSHADOW=(0.0625, -0.0625) IN; that is, the shadow or block extends 1/16th of an inch to the right and 1/16th of an inch below the legend.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.