As a discrete legend
gets more entries or as the legend entry text is lengthy, the legend
grows and the plot wall shrinks to accommodate the legend's size.
At some point, the plot wall becomes so small that it is useless.
For that reason, whenever all the legends in a graph occupy more than
20% of the total area of the graph, the larger legends are dropped
as needed from the graph to keep the legend area at 20% or less of
the graph area. For example, the following code generates only one
legend, but that legend would occupy more than 20% of the total area
of the graph, so the legend is dropped and the plot is rendered as
if no legend were specified.
proc template;
define statgraph legendsize;
begingraph;
entrytitle "Legend Drops out with GROUP=NAME";
layout overlay;
scatterplot x=Height y=Weight / name="sp" group=name;
discretelegend "sp" / title="Name" across=2 halign=right;
endlayout;
endgraph;
end;
run;
proc sort data=sashelp.class out=class; by name; run;
proc sgrender data=class template=legendsize;
run;
When the legend is dropped
from the graph, you see the following log note:
NOTE: Some graph legends have been dropped due to size constraints. Try adjusting
the MAXLEGENDAREA=, WIDTH= and HEIGHT= options in the ODS GRAPHICS
statement.
In such cases, you can use the WIDTH= and HEIGHT= options
in the ODS GRAPHICS statement to increase the graph area so that at
some point the legend is displayed.
Another alternative is to use the MAXLEGENDAREA=
option to change the threshold area for when legends drop out. The
following specification allows all legends to occupy up to 40% of
the graph area:
ods graphics / maxlegendarea=40;
proc sgrender data=class template=legendsize;
run;
However, changing the
total area that is allotted to legends might not resolve the problem
if the specified legend organization does not fit in the existing
size. In these cases, the legend might not be displayed and you would
see the following log message:
WARNING: DISCRETELEGEND statement with DISPLAYCLIPPED=FALSE is getting clipped.
The legend will not be drawn.
To investigate this problem, you
can specify DISPLAYCLIPPED=TRUE in the DISCRETELEGEND statement, which
forces the legend to display so that you can visually inspect it.
discretelegend "sp" / title="Name" across=2 halign=right displayclipped=true ;
In the current example,
it is apparent that the height chosen for the output is not large
enough to display the title and all legend entries in two columns.
The problem can be fixed in any of the following ways:
-
increasing the graph height (HEIGHT=
on ODS GRAPHICS statement or DESIGNHEIGHT= in the BEGINGRAPH statement)
-
relocating the legend and/or reorganizing
it with the ACROSS= or DOWN= options
-
setting DISPLAYCLIPPED=TRUE if
you are willing to see only a portion of the legend
-
reducing the font size for the
legend entries (and possibly the title).
To change the font sizes of the legend entries, use the
VALUEATTRS= option on the legend statement. To change the font size
of the legend title, use the TITLEATTRS= option. Normally, the legend
entries are displayed in 9pt font, and the legend title is displayed
in 10pt font. The following example reduces the size of legend text:
discretelegend "sp" / title="Name" across=2 halign=right
valueattrs=(size= 7pt) titleattrs=(size= 8pt);