Example Program and Statement Details

Example Graph

The following graph was generated by the Example Program:
Graph with a Discrete Legend

Example Program

proc template;
  define statgraph discretelegend;
    begingraph;
     entrytitle "Prediction Ellipses";
     layout overlayequated / equatetype=equate;
      scatterplot x=petallength y=petalwidth /
        group=species name="s";
      ellipse x=petallength y=petalwidth /
        type=predicted  alpha=.2
        name="p80" legendlabel="80%"
        outlineattrs=graphconfidence;
      ellipse x=petallength y=petalwidth /
        type=predicted  alpha=.05
        name="p95" legendlabel="95%"
        outlineattrs=graphconfidence2;
      discretelegend "s" / title="Species:";
      discretelegend "p80" "p95" / across=1
        autoalign=(topleft) location=inside;
     endlayout;
     entryfootnote halign=left "Fisher's Iris Data";
    endgraph;
  end;
run;

proc sgrender data=sashelp.iris template=discretelegend;
run;

Statement Summary

A discrete legend consists of one or more units called legend entries. Each legend entry consists of a legend symbol and a legend value. The legend symbol is typically a marker, line, or fill that represents a specific area in the plot. The legend value is descriptive text derived from group values or assigned with the plot’s LEGENDLABEL= option.
Within an overlay-type layout, when a discrete legend is placed inside the plot area with LOCATION=INSIDE,
  • It is always placed on top of plot lines and markers.
  • By default, its background is fully transparent ( OPAQUE=FALSE), meaning that underlying lines, markers, and data labels show through the legend.
  • Its position can be controlled with the AUTOALIGN= option, or with the HALIGN= and VALIGN= options.
Within an overlay-type layout, when a discrete legend is placed outside the plot area with LOCATION=OUTSIDE,
  • By default, its background is fully opaque (OPAQUE=TRUE).
  • Its position can be controlled with the HALIGN= and VALIGN= options.
When a discrete legend is placed within nested layouts, it might be necessary to use the ACROSS= and ORDER=ROWMAJOR options or the DOWN= and ORDER=COLUMNMAJOR options to obtain the desired legend organization. A legend might be dropped if the total legend area in the graph exceeds the percentage set by the MAXLEGENDAREA= option on an ODS GRAPHICS ON statement that is in effect for the output destination.

Required Arguments

graph-name
references one or more unique names that are specified by the NAME= option of a plot statement. These names control which plots contribute to the legend, and the order in which the legend entries occur. If a contributing plot uses a GROUP= option, then there is a legend entry for each group value.
  • Each graph-name must be enclosed in quotation marks.
  • Each plot that is referenced must be able to be identified by the legend entries. For example, a filled CONTOURPLOTPARM plot cannot be specified in the DISCRETELEGEND statement because it requires a continuous legend.

Options

Statement Option
Description
Specifies the number of legend entries that are placed horizontally before the next row begins.
Specifies whether the legend is automatically aligned within its parent layout when nested within an overlay-type layout.
Specifies the color of the legend background.
Specifies whether a border is displayed around the legend.
Specifies the properties of the border line around the legend.
Specifies whether the legend is displayed when any portion of the legend is not entirely rendered.
Specifies the number of legend entries that are placed vertically before the next column begins.
Specifies the horizontal alignment of the legend within its parent layout when nested within an overlay-type layout.
Specifies whether the legend appears inside or outside the plot area when the legend is specified within an overlay-type layout.
Specifies whether the legend background is opaque.
Specifies the order of the legend entries.
Specifies the amount of extra space that is added inside the legend perimeter.
Specifies the title of the legend.
Specifies the color and font attributes of the legend title.
Specifies a border around the legend title that separates the title from the legend body.
Specifies the vertical alignment of the legend within its parent layout when nested within an overlay-type layout.
Specifies the color and font attributes of the legend values.
ACROSS=positive-integer
specifies the number of legend entries that are placed horizontally before the next row begins.
Default: The entries are placed to best fit the available area. This “best fit” approach only works when the legend is nested in the template’s outermost layout.
Restriction: This option is ignored if ORDER=COLUMNMAJOR
A legend entry typically consists of two parts, such as a marker symbol and an associated value.
AUTOALIGN=NONE | AUTO | (location-list)
specifies whether the legend is automatically aligned within its parent layout when nested within an overlay-type layout. For more information about how child positions are determined in an overlay-type layout, see the LAYOUT OVERLAY’s Example Program and Statement Details.
Default: NONE
NONE
Do not automatically align the legend within its parent layout. The legend’s position is set by the HALIGN= and VALIGN= options.
AUTO
Available only if the parent layout contains a scatter plot; ignored otherwise. Within the parent layout, attempt to center the legend in the area that is farthest from any surrounding data point markers.
(location-list)
Within the parent layout, restrict the legend’s possible locations to those locations in the specified location-list, and use the location-list position that least collides with the parent layout’s other graphics features. The location-list is blank-separated and can contain any of these locations: TOPLEFT, TOP, TOPRIGHT, LEFT, CENTER, RIGHT, BOTTOMLEFT, BOTTOM, and BOTTOMRIGHT.
Interaction: This option has no effect unless LOCATION=INSIDE.
Interaction: If LOCATION=INSIDE, this option overrides HALIGN= and VALIGN=.
BACKGROUNDCOLOR=style-reference | color
specifies the color of the legend background.
Default: The GraphLegendBackground:Color style reference.
style-reference
A reference of the form style-element:style-attribute. Only the style-attribute named COLOR is used.
Interaction: OPAQUE=TRUE must be in effect for the color to be seen. By default, OPAQUE=FALSE.
BORDER=boolean
specifies whether a border is displayed around the legend.
Default: TRUE
BORDERATTRS=style-element | style-element (line-options) | (line-options)
specifies the attributes of the border line around the legend. See General Syntax for Attribute Options for the syntax on using a style-element and Line Options for available line-options.
Default: The GraphBorderLines style element.
Interaction: BORDER=TRUE must be set for this option to have any effect.
DISPLAYCLIPPED=boolean
specifies whether the legend is displayed when any portion of the legend is not entirely rendered.
Default: FALSE
When the graph size is reduced, parts of the legend (title, legend symbol, or legend value) might be clipped (truncated). When clipping occurs and DISPLAYCLIPPED is FALSE, the entire legend is removed from the graph and the space for it is reclaimed by the remainder of the graph. When DISPLAYCLIPPED is TRUE, the legend always appears, even if some parts of have been clipped.
DOWN=positive-integer
specifies the number of legend entries that are placed vertically before the next column begins.
Default: The entries are placed to best fit the available area. This “best fit” approach only works when the legend is nested in the template’s outermost layout.
Restriction: This option is ignored if ORDER=ROWMAJOR
A legend entry typically consists of two parts, such as a marker symbol and an associated value.
HALIGN=CENTER | LEFT | RIGHT
specifies the horizontal alignment of the legend within its parent layout when nested within an overlay-type layout. For more information about how child positions are determined in an overlay-type layout, see the LAYOUT OVERLAY’s Example Program and Statement Details.
Default: CENTER
Restriction: If LOCATION=OUTSIDE, the HALIGN= and VALIGN= options cannot both be set to CENTER.
Restriction: This option is available only when this statement is nested within an overlay-type layout. It is ignored if the parent layout is not an overlay-type layout.
Interaction: If LOCATION=INSIDE and the AUTOALIGN= option is enabled, this option is ignored.
LOCATION=OUTSIDE | INSIDE
specifies whether the legend appears inside or outside the plot area.
Default: OUTSIDE
Restriction: This option has effect only when the DISCRETELEGEND statement appears within an overlay-type layout and at least one stand-alone plot statement with XY axes is referenced by the legend. For more information about how child positions are determined in an overlay-type layout, see the LAYOUT OVERLAY’s Example Program and Statement Details.
Interaction: The actual position is determined by the settings for the LOCATION=, AUTOALIGN=, HALIGN=, and VALIGN= options.
OPAQUE=boolean
specifies whether the legend background is opaque (TRUE) or transparent (FALSE).
Default: TRUE when LOCATION=OUTSIDE FALSE when LOCATION=INSIDE
Interaction: When this option is set to FALSE, the background color is not used.
ORDER=ROWMAJOR | COLUMNMAJOR
specifies whether legend entries are added as rows or as columns.
Default: ROWMAJOR
Interaction: If ORDER=ROWMAJOR, use the ACROSS= option to limit the number of entries in a row. If ORDER=COLUMNMAJOR, use the DOWN= option to limit the number of entries in a column.
PAD=dimension | (pad-options)
specifies the amount of extra space that is reserved inside the legend perimeter.
Default: The default padding for all sides is 0. Values without units are in pixels (px). A unit must be provided if other than pixels.
dimension
Specifies a dimension to use for the extra space at the left, right, top, and bottom of the legend perimeter.
(pad-options)
Enables separate settings for the left, right, top, and bottom padding dimensions. Use the pad-options to create non-uniform padding. These options must be enclosed in parentheses. Each option is specified as a name = value pair. Sides not assigned padding are padded with the default amount.
Values without units are in pixels (px). A unit must be provided if other than pixels.
LEFT=dimension specifies the amount of extra space added to the left side.
RIGHT=dimension specifies the amount of extra space added to the right side.
TOP=dimension specifies the amount of extra space added to the top.
BOTTOM=dimension specifies the amount of extra space added to the bottom.
TITLE= "string"
specifies the title of the legend.
Default: no default
Requirement: string must be enclosed in quotation marks.
TITLEATTRS=style-element | style-element (text-options) | (text-options)
specifies the color and font attributes of the legend title. See General Syntax for Attribute Options for the syntax on using a style-element and Text Options for available text-options.
Default: The GraphLabelText style element.
Interaction: For this option to have any effect, the TITLE= option must also be specified.
TITLEBORDER=boolean
specifies a border around the legend title that separates it from the legend body.
Default: FALSE
Interaction: For this option to have any effect, the TITLE= option must also be specified.
The line attributes of the title border are set by the BORDERATTRS= options.
VALIGN=CENTER | TOP | BOTTOM
specifies the vertical alignment of the legend within its parent layout when nested within an overlay-type layout. For more information about how child positions are determined in an overlay-type layout, see the LAYOUT OVERLAY’s Example Program and Statement Details.
Default: CENTER
Restriction: If LOCATION=OUTSIDE, the VALIGN= and HALIGN= options cannot both be set to CENTER.
Restriction: This option is available only when this statement is nested within an overlay-type layout. It is ignored if the parent layout is not an overlay-type layout.
Interaction: If LOCATION=INSIDE and the AUTOALIGN= option is enabled, this option is ignored.
VALUEATTRS=style-element | style-element (text-options) | (text-options)
specifies the color and font attributes of the legend values. See General Syntax for Attribute Options for the syntax on using a style-element and Text Options for available text-options.
Default: The GraphValueText style element.