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

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;

SGPlot10.png

 

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;

SGPlot101.png

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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);

View solution in original post

5 REPLIES 5
RyanSimmons
Pyrite | Level 9

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.

Reeza
Super User
Because SAS is automatically adding some padding space so labels/text stay within the 'graph'.
DanH_sas
SAS Super FREQ

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

Rick_SAS
SAS Super FREQ

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);

Rick_SAS
SAS Super FREQ

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.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 1663 views
  • 14 likes
  • 4 in conversation