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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 5347 views
  • 1 like
  • 2 in conversation