Hello, I am using globallegend to represent my legends but unfortunately, the legend is coming outside the plot area. Is there any way to get the legend inside the graph? here is the code:
proc template;
define statgraph globallegend;
begingraph;
entrytitle "Prediction Ellipses";
layout overlay;
scatterplot x=petallength y=petalwidth / group=species
name="sp";
ellipse x=petallength y=petalwidth / type=predicted alpha=0.2
name="p80" legendlabel="80%" outlineattrs=graphconfidence;
ellipse x=petallength y=petalwidth / type=predicted alpha=0.05
name="p95" legendlabel="95%" outlineattrs=graphconfidence2;
endlayout;
layout globalLegend / type=column title="Sample Global Legend";
discretelegend "sp" / title="Species:";
discretelegend "p80" "p95" / title="Predictions:";
endLayout;
endgraph;
end;
run;
proc sgrender data=sashelp.iris template=globallegend;
run;
Please help!!
To do this, you need to use a LAYOUT GRIDDED inside of the LAYOUT OVERLAY:
proc template;
define statgraph globallegend;
begingraph;
entrytitle "Prediction Ellipses";
layout overlay;
scatterplot x=petallength y=petalwidth / group=species
name="sp";
ellipse x=petallength y=petalwidth / type=predicted alpha=0.2
name="p80" legendlabel="80%" outlineattrs=graphconfidence;
ellipse x=petallength y=petalwidth / type=predicted alpha=0.05
name="p95" legendlabel="95%" outlineattrs=graphconfidence2;
layout gridded / border=true autoalign=(topleft topright bottomleft bottomright);
entry halign=left "Sample Global Legend" / textattrs=GraphLabelText;
entry halign=left "Species:" / textattrs=GraphLabelText;
discretelegend "sp" / border=false halign=left;
entry halign=left "Predictions:" / textattrs=GraphLabelText;
discretelegend "p80" "p95" / border=false halign=left;
endlayout;
endlayout;
endgraph;
end;
run;
proc sgrender data=sashelp.iris template=globallegend;
run;
LAYOUT GLOBALLEGEND is outside of all other layouts by design. You can put both of your DISCRETELEGENDS inside of the LAYOUT OVERLAY and use the AUTOALIGN feature to have the legends position themselves in a way to prevent data collision (if possible). Would that work for you?
Did your layout overlay look like this?
layout overlay;
scatterplot x=petallength y=petalwidth / group=speciesname="sp";
ellipse x=petallength y=petalwidth / type=predicted alpha=0.2
name="p80" legendlabel="80%" outlineattrs=graphconfidence;
ellipse x=petallength y=petalwidth / type=predicted alpha=0.05
name="p95" legendlabel="95%" outlineattrs=graphconfidence2;
discretelegend "sp" / title="Species:" location=inside autoalign=(topleft topright bottomleft bottomright);
discretelegend "p80" "p95" / title="Predictions:" location=inside autoalign=(topleft topright bottomleft bottomright);
endlayout;
The LAYOUT GLOBALLEGEND block should be removed.
I get the picture below with the code I gave you. Is there a particular way you want the legends arranged?
To do this, you need to use a LAYOUT GRIDDED inside of the LAYOUT OVERLAY:
proc template;
define statgraph globallegend;
begingraph;
entrytitle "Prediction Ellipses";
layout overlay;
scatterplot x=petallength y=petalwidth / group=species
name="sp";
ellipse x=petallength y=petalwidth / type=predicted alpha=0.2
name="p80" legendlabel="80%" outlineattrs=graphconfidence;
ellipse x=petallength y=petalwidth / type=predicted alpha=0.05
name="p95" legendlabel="95%" outlineattrs=graphconfidence2;
layout gridded / border=true autoalign=(topleft topright bottomleft bottomright);
entry halign=left "Sample Global Legend" / textattrs=GraphLabelText;
entry halign=left "Species:" / textattrs=GraphLabelText;
discretelegend "sp" / border=false halign=left;
entry halign=left "Predictions:" / textattrs=GraphLabelText;
discretelegend "p80" "p95" / border=false halign=left;
endlayout;
endlayout;
endgraph;
end;
run;
proc sgrender data=sashelp.iris template=globallegend;
run;
Before the "Predictions:" entry, just add a " " entry:
entry halign=left " ";
entry halign=left "Predictions:" / textattrs=GraphLabelText;
You can do the same thing before "Species:" if you want a gap between the legend and the global title.
"Didn't work" is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.
In other words, Show the code you actually used and best from the log as the messages SAS provides often will indicate what is needed.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.