BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
n6
Quartz | Level 8 n6
Quartz | Level 8

I want to do a Y vs X scatterplot and I want to draw a 45 degree angle line on it so the viewer and easily see what points are above or below the diagonal.  Here is my code.

 

proc sgplot data=dataset;
scatter y=Y_var   x=X_Var;
lineparm x=0 y=0 slope=1 / legendlabel = '1 to 1 Line';
yaxis label = 'Y-axis Label';
xaxis label = 'X-axis Label';
run;

 

Here's the problem.  In the legend, in addition to labelling the 45 degree line as "1 to 1 line," it also puts the plotting symbol for the points and labels it with the variable name of the variable on the y-axis. So the legend looks like this:

 

"a circle" Y_var   "a line" 1 to 1 Line

 

It mystifies me why it would put the first part.  If you plot y vs x then I don't see a need to label the points with the name of the y-axis variable but SAS Help says that that indeed is what it is supposed to do.

 

But anyway, I looked and looked in the help for the SCATTER statement on how to undo that and couldn't figure it out then I came onto this site found the NOAUTOLEGEND statement.  Okay, that's good, I missed that because I was looking for options for the SCATTER line whereas NOAUTOLEGEND goes on the PROC SGPLOT line.

 

So I add NOAUTOLEGEND and the problem is that it takes away the legend entrely.  I want the legend to include only the second part, where it says "1 to 1 line" but it seems I get either whole legend including the first part which is unwanted or else I get no legend at all.

Any ideas on how to get a legend with only that second part?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Add the NAME option to each plot and a KEYLEGEND statement that only references the name of the plot(s) that you want to appear in the legend.

View solution in original post

3 REPLIES 3
ballardw
Super User

Add the NAME option to each plot and a KEYLEGEND statement that only references the name of the plot(s) that you want to appear in the legend.

n6
Quartz | Level 8 n6
Quartz | Level 8

 

Hey, that worked, thanks.  Actually I had already tried the NAME thing but I was usinig it incorrectly but your suggestion led me to try it again and now all is good.  Thanks again.

n6
Quartz | Level 8 n6
Quartz | Level 8

To be clear to other users, here is the code that successfully produces what I want.

 

proc sgplot data=dataset;
scatter y=Y_var   x=X_Var;
lineparm x=0 y=0 slope=1 / name='xyz'  legendlabel = '1 to 1 Line';
yaxis label = 'Y-axis Label';
xaxis label = 'X-axis Label';

keylegend 'xyz';
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 1996 views
  • 0 likes
  • 2 in conversation