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

Can I somehow modify the legend of the line graph (sgplot) to add information that one of the series has been drawn on the right axis?

 

Example:

 

data have;
 input year x1 x2;
 datalines;
 2000 10  200
 2001 15  190
 2002 12 210
 2003 12 220
 2004 10 210
;
run;

proc sgplot data=have;
 series x=year y=x1;
 series x=year y=x2/y2axis;
run;

 

Now, in the legend, I would like to add info that x2 is measured on the RHS, for example "x2 (RHS)". Can I do it without labelling the variable in the original dataset?

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

Sure.  Put whatever text you want.  If you want only one item, use the Keylegend statement shown.

 

proc sgplot data=have;
series x=year y=x1 / name='a' legendlabel='X1 - On Left Hand Y axis';
series x=year y=x2/y2axis name='b' legendlabel='X2 - On Right Hand Y axis';
keylegend 'b';
run;

 

Or, you can add an INSET.

View solution in original post

4 REPLIES 4
Jay54
Meteorite | Level 14

Sure.  Put whatever text you want.  If you want only one item, use the Keylegend statement shown.

 

proc sgplot data=have;
series x=year y=x1 / name='a' legendlabel='X1 - On Left Hand Y axis';
series x=year y=x2/y2axis name='b' legendlabel='X2 - On Right Hand Y axis';
keylegend 'b';
run;

 

Or, you can add an INSET.

chris2377
Quartz | Level 8

Thanks a lot. Easier than I expected. I somehow missed this option when I was looking at the syntax detail.

 

One additional question - can I do something similar with grouped graphs?

 

data have;
 input year group_name $1. x1 x2;
 datalines;
 2000 A 10 200
 2001 A 15 190
 2002 A 12 210
 2003 A 12 220
 2004 A 10 210
 2000 B 20 200
 2001 B 30 300
 2002 B 24 310
 2003 B 24 320
 2004 B 20 330
;
run;


On the graph there would be 4 series - two on the left axis and two on the right. I would like the legend to be something like that:
A - x1
A - x2 (RHS)
B - x1
B - x2(RHS)

 

Best

Jay54
Meteorite | Level 14

When the plot has a GROUP role, the legend contains the unique values of the group variable (like Male and Female for variable Sex). The LEGENDLABEL is ignored.  So, to get what you want, you can format the values from the variable to the strings you want to see in the legend.  Or, you can add a separate inset indicating which variables are being plotted on each axis.

chris2377
Quartz | Level 8
Thanks

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 5127 views
  • 1 like
  • 2 in conversation