Below, I have some code that produces some data and generates a plot:
%let Planned_E_Bt = 1.81;
%let Current_E_Bt = 0.82;
/* Generate lines */
DATA figure1;
do t = 0 to 1 by 0.01;
planned = &Planned_E_Bt*t;
current = &Current_E_Bt*t;
if t >= 0.5 then
do;
CP_current = current;
CP_planned = &Current_E_Bt*0.5 + &Planned_E_Bt*(t-0.5);
current = .;
end;
output;
end;
run;
/* Generate markers and labels */
DATA figure1;
set figure1;
if round(t,0.001)=0.5 then
do;
marker = CP_current;
laby1 = marker - 0.1;
label1 = "B(t=0.5)=" || strip(round(marker,0.001));
end;
if round(t,0.001)=0.4 then
do;
laby2 = planned + 0.2;
label2= "E{B(t)}=" || strip(round(&Planned_E_Bt,0.01)) || "t";
end;
run;
PROC SGPLOT data=figure1 noautolegend;
scatter x = t y = marker / markerattrs=(color=black);
series x = t y = planned / lineattrs=(color=black pattern=1);
series x = t y = current / lineattrs=(color=black pattern=2);
series x = t y = CP_current / lineattrs=(color=black pattern=1) curvelabel="CP=0.04" curvelabelloc=outside;
series x = t y = CP_planned / lineattrs=(color=black pattern=1) curvelabel="CP=0.41" curvelabelloc=outside;
yaxis label = "B(t)";
xaxis label = "t" values=(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0);
run;
This all works as intended. However, I am trying to add some labels as text to the plot. As soon as I add them (see modified SGPLOT code chunk below), it changes the x-axis. It adds white space at either end of the axis (<0 and >1).
PROC SGPLOT data=figure1 noautolegend;
scatter x = t y = marker / markerattrs=(color=black);
series x = t y = planned / lineattrs=(color=black pattern=1);
series x = t y = current / lineattrs=(color=black pattern=2);
series x = t y = CP_current / lineattrs=(color=black pattern=1) curvelabel="CP=&Current_CP." curvelabelloc=outside;
series x = t y = CP_planned / lineattrs=(color=black pattern=1) curvelabel="CP=&Planned_CP." curvelabelloc=outside;
text x = t y = laby1 text = label1;
text x = t y = laby2 text = label2;
yaxis label = "B(t)";
xaxis label = "t" values=(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0);
run;
How do I stop it from adding this blank space at either end? I can't figure out how to force it to not rescale the axis. Curvelabel doesn't work, because that will only let me add the labels to the beginning or end of the line instead of the middle. I've tried Datalabel, but that also adds this blank space at either end.
The space appears to account for the LENGTH of the label1 and label2 variables. You can override the space by using teh OFFSETMIN= and OFFSETMAX= options:
xaxis offsetmin=0.01 offsetmax=0.01 label = "t" values=(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0);
It seems that offsetmin and offsetmax can be used to get rid of this white space:
PROC SGPLOT data=figure1 noautolegend;
scatter x = t y = marker / markerattrs=(color=black);
series x = t y = planned / lineattrs=(color=black pattern=1);
series x = t y = current / lineattrs=(color=black pattern=2);
series x = t y = CP_current / lineattrs=(color=black pattern=1) curvelabel="CP=&Current_CP." curvelabelloc=outside;
series x = t y = CP_planned / lineattrs=(color=black pattern=1) curvelabel="CP=&Planned_CP." curvelabelloc=outside;
text x = t y = laby1 text = label1;
text x = t y = laby2 text = label2;
yaxis label = "B(t)";
xaxis label = "t" values=(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0) offsetmin=0 offsetmax=0;
run;
However, this seems a little clunky (e.g. if I have curvelabelloc=inside, using offsetmax will cut off the curve label instead of displaying it). And even with this workaround I would still like to better understand exactly WHY the text option is causing the axis to be rescaled the way it is.
Actually, instead of using axis offsets to fix this issue, you'll want to set CONTRIBUTEOFFSETS=NONE on the TEXT plots. That way, if your data changes, you'll still get the behavior you want without having to adjust the offsets.
Hope this helps!
Dan
The space appears to account for the LENGTH of the label1 and label2 variables. You can override the space by using teh OFFSETMIN= and OFFSETMAX= options:
xaxis offsetmin=0.01 offsetmax=0.01 label = "t" values=(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0);
By the way, THANK YOU a hundred times for taking the time to create an easily reproducible example! It made answering the question quick and easy I wish all the questions were as well constructed as yours is.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.