SGPLOT Procedure

KEYLEGEND Statement

Adds a legend to the plot.
Adding a Prediction Ellipse to a Scatter PlotCreating Lines and Bands from Pre-Computed DataCombining Histograms with Density Plots

Syntax

KEYLEGEND <“name–1” ... “name-n”> </ option(s)>;

Summary of Optional Arguments

Legend options
specifies the number of columns in the legend.
specifies whether the border around the legend is visible.
specifies the number of rows in the legend.
specifies whether the legend is placed outside or inside of the axis area.
specifies the position of the legend within the graph.
specifies the appearance of the legend value labels.
specifies the names of one or more plots that you want to include in legend.
Title options
specifies a title for the legend.
specifies the appearance of the title.

Optional Arguments

name-1” ... “name-n
specifies the names of one or more plots that you want to include in legend. Each name that you specify must correspond to a value that you entered for the NAME= option in a plot statement.
Note: If you do not specify a name, then the legend contains references to all of the plots in the graph.
ACROSS= n
specifies the number of columns in the legend. By default, the number of columns is determined automatically.
Note:Depending on the number of legend entries and the number of columns and rows that you specify, the legend might not fit in your graph. If your legend does not appear, then you might need to specify a different value for the ACROSS= option.
BORDER | NOBORDER
specifies whether the border around the legend is visible. The BORDER option shows the border. The NOBORDER option hides the border.
Default:BORDER
DOWN= n
specifies the number of rows in the legend. By default, the number of rows is determined automatically.
Note: Depending on the number of legend entries and the number of columns and rows that you specify, the legend might not fit in your graph. If your legend does not appear, then you might need to specify a different value for the DOWN= option.
LOCATION= OUTSIDE | INSIDE
specifies whether the legend is placed outside or inside of the axis area. The OUTSIDE option places the legend outside of the axis area. The INSIDE option places the legend inside of the axis area.
Default:OUTSIDE
Interaction:If LOCATION=INSIDE and POSITION= is also specified with any value, you might need to offset the axis in order to make space for the legend within the axis area. For more information, see Legend with a Position Specified inside the Axis Area.
POSITION= position-value
specifies the position of the legend within the graph. The positions are as follows:
BOTTOM
places the legend at the bottom of the graph.
BOTTOMLEFT
places the legend at the bottom left corner of the graph.
BOTTOMRIGHT
places the legend at the bottom right corner of the graph.
LEFT
places the legend at the left side of the graph.
RIGHT
places the legend at the right side of the graph.
TOP
places the legend at the top of the graph.
TOPLEFT
places the legend at the top left corner of the graph.
TOPRIGHT
places the legend at the top right corner of the graph.
Default:BOTTOM

If LOCATION=INSIDE is specified, the legend is automatically positioned near an edge of the plot area that has the least amount of collision with the data.

Interaction:If LOCATION=INSIDE and POSITION= is also specified with any value, you might need to offset the axis in order to make space for the legend within the axis area. For more information, see Legend with a Position Specified inside the Axis Area.
Notes:By default, if you use more than one KEYLEGEND statement, then each legend is placed in a different position.

If you specify more than one legend with the same position, then all of your legends are placed at that position.

TITLE= “text-string
specifies a title for the legend.
TITLEATTRS= style-element <(options)> | (options)
specifies the appearance of the title. You can specify the appearance by using a style element or by using suboptions. If you specify a style element, you can also specify suboptions to override specific appearance attributes.
Here is an example that specifies a style element: TITLEATTRS=GraphTitleText
options can be one or more of the following:
COLOR= color
specifies the color of the title characters. You can specify colors using the same color schemes that are supported by SAS/GRAPH software. For more information, see Color-Naming Schemes in SAS/GRAPH: Reference.
Default:For ungrouped data, the default color is specified by the Color attribute of the GraphLabelText style element in the current style. For grouped data, the default color is specified by the ContrastColor attribute of the GraphData1 ... GraphDatan style elements in the current style.
FAMILY= “font-family
specifies the font family for the title characters. The SAS ODS styles use TrueType system fonts. For more information, see TrueType Fonts Supplied by SAS in SAS Language Reference: Concepts.
Default:The default font family is specified by the FontFamily attribute of the GraphLabelText style element in the current style.
Restriction:You cannot specify a list of font family names.
SIZE= n <units>
specifies the font size of the title characters. You can also specify the unit of measurement. The default unit is pixels. For a list of measurement units that are supported, see Units of Measurement.
Default:The default font size is specified by the FontSize attribute of the GraphLabelText style element in the current style.
STYLE= ITALIC | NORMAL
specifies whether the title characters are italic or normal.
Default:The default font style is specified by the FontStyle attribute of the GraphLabelText style element in the current style.
WEIGHT= BOLD | NORMAL
specifies whether the title characters are bold or normal.
Default:The default font weight is specified by the FontWeight attribute of the GraphLabelText style element in the current style.
Here is an example expression:
TITLEATTRS=(Color=Green Family=Arial Size=8 Style=Italic Weight=Bold)
Default:The default appearance of the title text is specified by the GraphLabelText style element in the current style.
VALUEATTRS= style-element <(options)> | (options)
specifies the appearance of the legend value labels. You can specify the appearance by using a style element or by using suboptions. If you specify a style element, you can also specify suboptions to override specific appearance attributes.
options can be one or more of the following:
COLOR= color
specifies the color of the label characters. You can specify colors using the same color schemes that are supported by SAS/GRAPH software. For more information, see Color-Naming Schemes in SAS/GRAPH: Reference.
Default:For ungrouped data, the default color is specified by the Color attribute of the GraphValueText style element in the current style. For grouped data, the default color is specified by the ContrastColor attribute of the GraphData1 ... GraphDatan style elements in the current style.
FAMILY= “font-family
specifies the font family for the label characters. The SAS ODS styles use TrueType system fonts. For more information, see TrueType Fonts Supplied by SAS in SAS Language Reference: Concepts.
Default:The default font family is specified by the FontFamily attribute of the GraphValueText style element in the current style.
Restriction:You cannot specify a list of font family names.
SIZE= n <units>
specifies the font size of the label characters. You can also specify the unit of measurement. The default unit is pixels. For a list of measurement units that are supported, see Units of Measurement.
Default:The default font size is specified by the FontSize attribute of the GraphValueText style element in the current style.
STYLE= ITALIC | NORMAL
specifies whether the label characters are italic or normal.
Default:The default font style is specified by the FontStyle attribute of the GraphValueText style element in the current style.
WEIGHT= BOLD | NORMAL
specifies whether the label characters are bold or normal.
Default:The default font weight is specified by the FontWeight attribute of the GraphValueText style element in the current style.

Examples

Example 1: Legend for Two Ellipses

Keylegend example
ods graphics / reset width=4in;
proc sgplot data=sashelp.class
      noautolegend;
  scatter x=height y=weight;
  ellipse x=height y=weight /
     alpha=.2
     name="eighty"
     legendlabel="80% Prediction";
  ellipse x=height y=weight /
     alpha=.05
     name="ninetyfive"
     legendlabel="95% Prediction";
  keylegend "eighty" "ninetyfive";
run;

Example 2: Legend with a Position Specified inside the Axis Area

If POSITION= and LOCATION= INSIDE are both specified, you might need to offset the axis in order to make space for the legend within the axis area.
The following example specifies LOCATION= INSIDE and POSITION= BOTTOM. In the graph, the legend collides with the data. To avoid this problem, the graph specifies an OFFSETMIN= value for the Y axis. (If the legend’s position was at the top of the plot, an OFFSETMAX= value would be specified instead.)
Plot with Y axis offset at the bottom of the axis
ods graphics / reset width=4in;
title "Mileage by Type (Excluding Hybrids)";
proc sgplot data=sashelp.cars;
  where type ne "Hybrid";
  vbar type / response=mpg_city;
  keylegend / location=inside position=bottom;
  xaxis grid;
  yaxis offsetmin=0.15 display=(nolabel);
run;
title;
Note: For HBOX, HBAR, HLINE, and DOT plot statements, the Y axis is reversed by default, so the axis origin is at the top. As a result, you use OFFSETMAX= to create an offset at the bottom of the axis. OFFSETMIN= is used to create an offset at the top of the axis for these plot types.
The following example specifies LOCATION= INSIDE and POSITION= TOPRIGHT. To avoid having the legend collide with the graph, the code specifies an OFFSETMAX= value for the X axis. The OFFSETMAX= value is applied to the end of the axis farther from the origin.
Plot with X axis offset to the right of the axis
ods graphics / reset width=4in;
title "Average Height of Teenage Students";
proc sgplot data=sashelp.class;
  where age >= 12 and age < 16;
  hbar age / response=height stat=mean;
  keylegend / location=inside 
    position=topright;
  xaxis offsetmax=0.3 display=(nolabel);
  yaxis grid;
run;
title;