kelsz24
kelsz24

Reputation: 23

Trying to create prevalence chart with error bars and percentages

I am trying to create a bar graph that has error bars showing the upper and lower confidence limits. However all my code will not produce the desired error bars. I would also like to include the prevalence percentages in the graph.

This is the code I have that inputs the data

data disabilities;
input disability $ prevalence lower upper;
datalines;
Physical 86.38 84.75 88.01
Cognitive 83.42 81.99 84.85
Both 80.71 78.23 83.19
No 88.26 87.76 88.76;
run;

I then run the following code to produce a bar graph

proc sgplot data=disabilities;  
vbarparm category=disability response=prevalence / group=disability fillpattern;  
band x=disability lower=lower upper=upper / group=disability transparency=0.5; 
xaxis discreteorder=datapart;
run;

Which produces this graph without the error bars

I am trying to achieve something similar to this:

enter image description here

Any help is appreciated!!

Upvotes: 0

Views: 476

Answers (2)

Don
Don

Reputation: 1

Be aware of potential conflict when label overlay plots. Please try following codes:

data disab;
    length label $8;
    set disabilities;
    label = strip(prevalence)||"%" ;
run;


ods _all_ close;
ods graphics on/reset;
ods rtf;

proc sgplot data=disab;  
    vbarparm category =disability  response=prevalence 
        /group=disability fillpattern GROUPDISPLAY=CLUSTER  
    ;        
    Highlow  High = upper low = lower x = disability/   
        highcap =serif lowcap = serif lineattrs = 
        Grapherror 
        highlabel= label;
    yaxis offsetmin = 0.05 offsetmax = 0.05  ;
ods _all_ close;
ods listing;

Upvotes: 0

Quentin
Quentin

Reputation: 6378

You can label the bars with the datalabel option, and add limits with limitupper / limitlower. Unfortunately when you use both it labels the bars and the limits, and puts the values below the bars, in a table.

proc sgplot data=disabilities;  
vbarparm category=disability response=prevalence
  / group=disability groupdisplay=cluster fillpattern 
    datalabel limitlower=Lower limitupper=Upper datalabelpos=data
;  
xaxis discreteorder=data;
run;

For a plot closer to what you want, you might be able to get it from SGPLOT overlaying multiple plots (one for the bars, another for the error bars). Or with GTL.

Personally, I'm not a fan of labeling values in a bar chart (or most charts). It's a chart, not a table. And in the example you showed, the labels have the value of the bar, but they're positioned at the top of the error bar, which is confusing.

Upvotes: 1

Related Questions