BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
v307086
Calcite | Level 5

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;

 

Capture.PNG

 

Please help!!

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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;

iris2.png

View solution in original post

13 REPLIES 13
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10
Have you tried the LOCATION=INSIDE option statement in the DISCRETELEGEND statement?

https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/grstatgraph/p0nebkqa1obtgxn13ska62up7wwh.h...
v307086
Calcite | Level 5
Yes, I have tried it, but no luck
DanH_sas
SAS Super FREQ

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?

v307086
Calcite | Level 5
Hello DanH_sas,
I tried this as well, but it didn't work. Any other suggestion
DanH_sas
SAS Super FREQ

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.

v307086
Calcite | Level 5
Yeah I did the same, and removed layout globallengend block
DanH_sas
SAS Super FREQ

I get the picture below with the code I gave you. Is there a particular way you want the legends arranged?

 

iris.png

 

v307086
Calcite | Level 5
I want my legends to align vertically.
Like this:
Species
symbol Setosa
symbol Versicolor
symbol Viginica

Predictions
symbol 80%
symbol 95%

Along with the vertical alignment, i want them to be placed on the right side of the plot.
DanH_sas
SAS Super FREQ

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;

iris2.png

v307086
Calcite | Level 5
Thanks DanH_sas, it worked!!
Just a quick question what option do I need to use if I want a space between viriginica and predications. like i want to show that there are two different titles. A space will work i think. Any leads?
DanH_sas
SAS Super FREQ

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.

v307086
Calcite | Level 5
I tried this but it didn’t worked for me. Any other solution?
ballardw
Super User

"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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1343 views
  • 3 likes
  • 4 in conversation