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.
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;
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.
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:
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:
Let me know if you have any questions or issues with these approaches.
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?
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;
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;
Text plot worked. Thanks
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.