BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I am using proc template to generate this kind of graph. What is the option available to auto move the text inside the bars so that it wont collide with error bar graph line. Thank you.

SASuserlot_0-1717169253430.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

I think your two best options are the second item I mentioned (except you should use "bottom" instead of "top"), or use an AXISTABLE. Here are the code snippets for both:

 

TEXTPLOT

data temp;
set chek;
length pos $ 6;
if q1 < (median / 2) then do;
   LabelY = q1;
   pos = "bottom";
end;
else do;
   LabelY = median / 2;
   pos = "center";
end;
run;

   proc template;
      define statgraph template1;
      BeginGraph;
         layout overlay/xaxisopts=(type=discrete offsetmin=0.22 offsetmax=0.22 );
         barchart x=groupn y=median/group=paramcdn groupdisplay=cluster
                  clusterwidth=0.4 barwidth=0.8;
         scatterplot x=groupn y=median/yerrorlower=q1 yerrorupper=q3
                     group=paramcdn groupdisplay=cluster ;
         textplot x=groupn y=LabelY text=median / position=pos strip=true 
                     group=paramcdn groupdisplay=cluster ;
         endlayout;
      EndGraph;
      end;
   run;

   proc sgrender data=temp template=template1;
      format groupn fail. paramcdn grade.;
   run;

AXISXTABLE

proc template;
      define statgraph template2;
      BeginGraph;
         layout overlay/xaxisopts=(type=discrete offsetmin=0.22 offsetmax=0.22 );
         barchart x=groupn y=median/group=paramcdn groupdisplay=cluster
                  clusterwidth=0.4 barwidth=0.8;
         scatterplot x=groupn y=median/yerrorlower=q1 yerrorupper=q3
                     group=paramcdn groupdisplay=cluster ;
         innermargin / align=bottom;
            axistable x=groupn value=median / class=paramcdn classdisplay=cluster;
         endinnermargin;
         endlayout;
      EndGraph;
      end;
   run;

   proc sgrender data=chek template=template2;
      format groupn fail. paramcdn grade.;
   run;

View solution in original post

5 REPLIES 5
ballardw
Super User

You should include the code defining your template so we can see all of the options in effect.

 

Depending on your code/date there are multiple ways to draw "error bars" and not all of them would support the same options, plus need to know the current setting that might effect this.

 

For a BARCHARTPARM you might try setting the ERRORBARCAPSHAPE=NONE so there is no line across the value.

 

If using a HIGHLOWPLOT to overlay error bars you could use the LOWCAP=NONE to suppress caps just on the lower. Or add a variable that contains the type of cap to display and use NONE for just the X values that are having the problem.

Or adjust the value of the lower bar variable

Or use TEXTPLOT to display the labels and adjust the location.

DanH_sas
SAS Super FREQ

I'm going to assume you are using a BARCHARTPARM statement, with ERRORUPPER and ERRORLOWER and SEGMENTLABELTYPE. The answer to your question depends on the behavior you want. If you want the value to always be right below the error bar, then do the following:

  1. Remove the ERRORUPPER, ERRORLOWER, and SEGMENTLABELTYPE options.
  2. Add a HIGHLOWPLOT after the BARCHARTPARM, assigning the upper and lower error variable to the plot, adding HIGHCAP=serif, LOWCAP=serif, and  the bar value variable to the LOWLABEL option.

If your goal is to keep the value in the center of the bar, unless the error bar "pushes" it down, you would need to do the following:

  1. Create a temporary data set that will add a numeric column for the Y location for the labels (let's call it LabelY) and a string column for text anchor positions (let's call it POS) to the original data. In the data step, the LabelY value should be the lesser of the 1/2 bar value or the lower error value. If you use the bar value, assign "CENTER" to the POS column, else assign "TOP". You will use this data set instead of the original data set.
  2. Remove the SEGMENTLABELTYPE option.
  3. Add a TEXTPLOT after the BARCHARTPARM, using the LabelY column for the Y option and the POS column on the POSITION option.

Let me know if you have any questions or issues with these approaches.

SASuserlot
Barite | Level 11

Thank you @DanH_sas @ballardw  for your responses. I tried to recreate the code for what I am looking. in the first bar median number label inside the bar got obstructed by the lower error bar, How I can avoid that, I believe median values always try to fit in the middle of the bar. is it possible to avoid the overwriting?

SASuserlot_0-1717181975045.png

 


proc format;
   value fail
      1='fail'
      2='pass';
   value grade
      1='11grade'
      2='10grade';
run;
data chek;
median =0.60;
  q1 = 0.25;
  q3 =1.5;
  mean =1.02;
  group = 'One';
      groupn =1;
	  x=0.2;
	    n =25;

	  paramcd = 'FAIL';
	  paramcdn =1;
	  min =0;
	  max =2;
  output;



median =0.25;
  q1 = 0.36;
  q3 =1.5;
  mean =1.02;
  group = 'TWO';
      groupn =2;
	  x=0.2;
	    n =25;
	  min =0;
	  max =2;
	  paramcd = 'FAIL';
	  paramcdn =1;
  output;

run;

ods listing;
   ods graphics/reset imagefmt=png height=5in width=8in ;	
 
   proc template;
      define statgraph template1;
      BeginGraph;
 
 
         layout overlay/xaxisopts=(type=discrete 
										 offsetmin=0.22 offsetmax=0.22 );

 
         barchart x=groupn y=median/group=paramcdn groupdisplay=cluster 
                               clusterwidth=0.4 barwidth=0.8
							   segmentlabel=true  BARLABELFITPOLICY=INSIDEPREFERRED;
		 scatterplot x=groupn y=median/yerrorlower=q1 yerrorupper=q3 
					 group=paramcdn groupdisplay=cluster ;
 
 
 
         endlayout;
      EndGraph;
      end;
   run;

   proc sgrender data=chek template=template1;
      format groupn fail. paramcdn grade.;
   run;
 
ods listing close;
DanH_sas
SAS Super FREQ

I think your two best options are the second item I mentioned (except you should use "bottom" instead of "top"), or use an AXISTABLE. Here are the code snippets for both:

 

TEXTPLOT

data temp;
set chek;
length pos $ 6;
if q1 < (median / 2) then do;
   LabelY = q1;
   pos = "bottom";
end;
else do;
   LabelY = median / 2;
   pos = "center";
end;
run;

   proc template;
      define statgraph template1;
      BeginGraph;
         layout overlay/xaxisopts=(type=discrete offsetmin=0.22 offsetmax=0.22 );
         barchart x=groupn y=median/group=paramcdn groupdisplay=cluster
                  clusterwidth=0.4 barwidth=0.8;
         scatterplot x=groupn y=median/yerrorlower=q1 yerrorupper=q3
                     group=paramcdn groupdisplay=cluster ;
         textplot x=groupn y=LabelY text=median / position=pos strip=true 
                     group=paramcdn groupdisplay=cluster ;
         endlayout;
      EndGraph;
      end;
   run;

   proc sgrender data=temp template=template1;
      format groupn fail. paramcdn grade.;
   run;

AXISXTABLE

proc template;
      define statgraph template2;
      BeginGraph;
         layout overlay/xaxisopts=(type=discrete offsetmin=0.22 offsetmax=0.22 );
         barchart x=groupn y=median/group=paramcdn groupdisplay=cluster
                  clusterwidth=0.4 barwidth=0.8;
         scatterplot x=groupn y=median/yerrorlower=q1 yerrorupper=q3
                     group=paramcdn groupdisplay=cluster ;
         innermargin / align=bottom;
            axistable x=groupn value=median / class=paramcdn classdisplay=cluster;
         endinnermargin;
         endlayout;
      EndGraph;
      end;
   run;

   proc sgrender data=chek template=template2;
      format groupn fail. paramcdn grade.;
   run;
SASuserlot
Barite | Level 11

Text plot worked. Thanks

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 493 views
  • 2 likes
  • 3 in conversation