Data visualization with SAS programming

PROC SGPLOT and its legend

Accepted Solution Solved
Reply
Contributor n6
Contributor
Posts: 62
Accepted Solution

PROC SGPLOT and its legend

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?


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Grand Advisor
Posts: 10,251

Re: PROC SGPLOT and its legend

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


All Replies
Solution
‎09-25-2015 06:23 AM
Grand Advisor
Posts: 10,251

Re: PROC SGPLOT and its legend

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.

Contributor n6
Contributor
Posts: 62

Re: PROC SGPLOT and its legend

 

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.

Contributor n6
Contributor
Posts: 62

Re: PROC SGPLOT and its legend

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 471 views
  • 0 likes
  • 2 in conversation