Placing labels on the top of a bar in a bar chart

From PHUSE Wiki
Jump to: navigation, search
SaschaAhrweiler 10:26, 31 January 2012 (CST)
Labelbarchart.JPG

Description

You want to have a nice overview in bar charts and somehow have annotated charts which include the number of subjects for each group?

This little example will show how this can easily be done with ANNOTATE and use the dataset which was created for the computation of the percentages.

Approach

There are multiple ways to solve this issue. One easy solution is the following:

  • The label "n = " has to be printed by using an ANNOTATE data set.
  • The dataset that was created for the computation of the percentages can be used to fill the number next to the label

Example for Program Specifications

Calculation of Percent and n

This dataset is used as input dataset for the PROC GCHART and for the ANNOTATE dataset.

 …
data plt; 
     merge stat(in=eff) nges nv2; 
     by visit;
     keep qstestcd qsstresn proz qsscat n;
     if eff;
     proz=n/nv*100;
run;
...

Annotate Specifications

data anno; 
     set plt;
     length function $5 text $10;
	 xsys='2'; 
     ysys='2';
	 position='2';
	 y=proz;
 	 x=0;
	 Midpnt=qsstresn;
	 function='label';
     text='n = ' || strip(put(n,best.));
run;

It is necessary to assign a ‘2’ to the variables XSYS and YSYS in order to refer to the original plotting scale. The variable MIDPNT defines the midpoints of the bars, in this case identified by the variable QSSTRESN. Y is the value of the percentages calculated for each bar and is therefore the point on the y-axis up to which the bar is drawn or where the label has to be placed, respectively.

PROC GCHART

proc gchart data=plt annotate=anno;
     axis2    label =  none 
	          order = (0 to 100 by 10)
			 length=  55pct
			 origin= (5pct,20.6pct)
			 value = (color=black h=0.75 
			           t=1 '0%' t=2 '10%' t=3 '20%' t=4 '30%' t=5 '40%' t=6 '50%' t=7 '60%' t=8 '70%'     t=9 '80%' t=10 '90%' t=11 '100%');
axis1   label = none
	          origin = (5pct,20.6pct) 
			 offset = (10pct,10pct)
	          value=  (color=black h=0.75
			          t=1 '1' j=c 'I agree' j=c 'completely'
					  t=6 '6' j=c "I don't"  j=c "agree at all")
			 order =  (1 to 6 by 1);
	 
	  vbar    qsstresn / sumvar=proz
	 				   midpoints=1 2 3 4 5 6
					   nolegend
					   gspace=6
					   width=12
					   noframe
					   maxis=axis1
					   axis=axis2;

run;