Is there a way to use %sgtext to create new y axis labels in sgplot? I'm trying to do two things:
1. get my y1 axis label to display a phrase in a different color: for example the y1 should say: "Managers and" in blue and "employees" in red.
2. get my y2 axis label to rotate facing downward. Right now both my y lables read upward. I want the y1 label to stay that way, but the y2 label to rotate so that it reads from top to bottom.
I tried a variety of %sgtext using the layoutpercent drawspace but I keep getting errors in the log saying I'm failing to define certain axes. Does anyone know how to do tthis sort of thing?
Here is a simple example using a variation of your macro content. The key is choosing the best drawing space for your annotation. Here, the axis label is positioned relative to the wall, so I used WALLPERCENT with x=108% (beyond the right edge of the wall) and y=50% (the middle of the wall). I also set the anchor to be the center of the text box. I also had to set the width to prevent wrapping, and I used -90 degree rotation. Give it a try and see if it is what you want.
Thanks!
Dan
%SGANNO
data jp_anno3;
%SGTEXT(
LABEL="Employment"
, ANCHOR= "center"
, TEXTCOLOR="green"
, width=40
, X1SPACE="wallpercent"
, y1=50
, y1space="wallpercent"
, x1=108
, LAYER="front"
, textsize=9
, textweight="bold"
, rotate=-90
)
run;
proc sgplot data=sashelp.class sganno=jp_anno3 pad=(right=30px);
y2axis display=(nolabel);
scatter x=age y=weight;
scatter x=age y=height / y2axis;
run;
It will help to show code for what you have so far, both the annotation and the graphics procedure.
My plot is a typical SGPLOT. My first attempt at anno is to flip the y2 axis label. It didn't work, so I haven even attempted to create another one with split colors:
%SGANNO
data jp_anno3;
%SGTEXT(
LABEL="Employment"
, ANCHOR= "right"
, TEXTCOLOR="green"
, X1SPACE="layoutpercent"
, y2=emp
, y2space="datavalue"
, x1=-11
, LAYER="front"
, textsize=9
, textweight="bold"
, rotate=180
)
run;
/*Chart3*/
ods pdf startpage=never;
ods graphics / reset width=700px height=600px imagename="gridsgplot" imagefmt=gif border=on;
ods escapechar='^';
proc sgplot data=c_jo pad=(bottom=2pct top=2pct) sganno=jp_anno3 noautolegend;
TITLE1 font="helvetica/bold" HEIGHT=1.6 JUSTIFY=LEFT " Chart 3. First Title";
TITLE2 font="helvetica/bold" HEIGHT=1.3 JUSTIFY=LEFT " Subtitle";
/* band can highlight a portion of the graph in this case the most recent recession*/
band y=r1 lower="01DEC07"d upper="01JUN09"d / transparency=.8 fillattrs=(color=gray4C);
series x=sasdate y=hi / lineattrs=(color=blue) name="hi" legendlabel="Employees";
series x=sasdate y=ts / lineattrs=(color=red pattern=shortdash thickness=2) name="ts" legendlabel="Managers";
series x=sasdate y=emp / y2axis lineattrs=(color=green pattern=dash thickness=2) name="emp" legendlabel="Total Staff";
xaxis type=time
ranges=("01JAN2005"d - "01aug2015"d)
display=(novalues) label=' ' LABELATTRS=(Size=18)
offsetmin=0.0 offsetmax=0.0
;
yaxis label = 'Managers and Employees’ LABELATTRS=(Color=blue Family=Arial Size=8 Weight=Bold)
grid values = (3000 to 6500 by 500) valueattrs=(size=8)
offsetmin=0.0001 offsetmax=0.001
;
y2axis label = 'Total Staff' LABELATTRS=(Color=Green Family=Arial Size=8 Weight=Bold)
grid values = (129000 TO 143000 by 2000)valueattrs=(size=8)
offsetmin=0.0001 offsetmax=0.001
;
keylegend "hi" "ts" "emp" / valueattrs=(size=7 weight=bold) location=outside position=bottom noborder;
run;
Here is a simple example using a variation of your macro content. The key is choosing the best drawing space for your annotation. Here, the axis label is positioned relative to the wall, so I used WALLPERCENT with x=108% (beyond the right edge of the wall) and y=50% (the middle of the wall). I also set the anchor to be the center of the text box. I also had to set the width to prevent wrapping, and I used -90 degree rotation. Give it a try and see if it is what you want.
Thanks!
Dan
%SGANNO
data jp_anno3;
%SGTEXT(
LABEL="Employment"
, ANCHOR= "center"
, TEXTCOLOR="green"
, width=40
, X1SPACE="wallpercent"
, y1=50
, y1space="wallpercent"
, x1=108
, LAYER="front"
, textsize=9
, textweight="bold"
, rotate=-90
)
run;
proc sgplot data=sashelp.class sganno=jp_anno3 pad=(right=30px);
y2axis display=(nolabel);
scatter x=age y=weight;
scatter x=age y=height / y2axis;
run;
Thanks, Dan. This worked perfectly for the rotation. What would your suggestion be to split a yaxis label into two different colors ("Managers and" in blue and "employees" in red. Would it involve two different sgtext steps?
Yes, add this macro after the other %SGTEXT macro in your data step:
%SGTEXTCONT(
LABEL=" Opportunity"
, TEXTCOLOR="red"
, textsize=9
, textweight="bold"
)
Thank you, Dan. Both of these pieces of code work perfectly.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: