[R] Barplots and error indicators: Some R-Code

Frank E Harrell Jr f.harrell at vanderbilt.edu
Fri Jun 18 16:48:03 CEST 2004


dieter.haering at fal.admin.ch wrote:
> I' ve seen that several people are looking for a function that creates a
> barplot with an error indicators (I was one of them myself). Maybe you will
> find the following code helpful (There are some examples how to use it at
> the end):
> 
> 
> # Creates a barplot. 
> #bar.plot() needs a datavector for the height of bars and a error
> #indicator for the interval 
> #many of the usual R parameters can be set: e.g. ylim, main, col, etc.
> #The direction of the error indicator can be specified as "bo" for both,
> "lo" lower, "up"  #upper
> #The width of the indicator hat is set as a percentage of the x-width of the
> plot:e.g.  *hat=0.05
> 
> bar.plot<-function(data, err.ind, ind.side=NA, hat=NA, ylim=c(0,
> max(data+err.ind,  na.rm=TRUE)*1.2), col=NA, xlab="your x-lab",  ylab="your
> y-lab", main="your main",  names.arg=NA, space=0.2){
> if (is.na(ind.side)){
> ind.side<-"up"}	#only upper error indicator (default), else: "lo" or "bo"
> if (is.na(hat)){
> hat<-0.01}	#fraction of the x axis that defines the size of the error
> indicator hat
> if (is.na(col)){col<-"white"} #the default color is white
> x.cor<-barplot(data, ylim=ylim, xlab=xlab, ylab=ylab, main=main,
> names.arg=names.arg,   col=col, space=space)
> smidge<-diff(par("usr")[1:2])*hat
> up.ind<-data+err.ind
> lo.ind<-data-err.ind
> segments(0,0,x.cor,0)	# draws X-Axis
> if (ind.side=="up"){
> segments(x.cor, data, x.cor, up.ind)
> segments(x.cor, up.ind, x.cor+smidge, up.ind)
> segments(x.cor, up.ind, x.cor-smidge, up.ind)
> }else{
> if (ind.side=="bo"){
> segments(x.cor, data, x.cor, up.ind)
> segments(x.cor, up.ind, x.cor+smidge, up.ind)
> segments(x.cor, up.ind, x.cor-smidge, up.ind)
> segments(x.cor, data, x.cor, lo.ind)
> segments(x.cor, lo.ind, x.cor+smidge, lo.ind)
> segments(x.cor, lo.ind, x.cor-smidge, lo.ind)
> }else{
> segments(x.cor, data, x.cor, lo.ind)
> segments(x.cor, lo.ind, x.cor+smidge, lo.ind)
> segments(x.cor, lo.ind, x.cor-smidge, lo.ind)
> }
> }
> }
> 
> 
> #Examples
> par(mfrow=c(2,2))
> 
> data<-c(3.2, 3.3, 3.6, 4, 3.1, 3.3, 3.1, 4.5, 3.2, 3.3, 3.1, 3.4)
> se<-sqrt(data)
> bar.plot(data, se) #easy example
> 
> data<-c(3.2, 3.3, 3.6, 4, 3.1, 3.3, 3.1, 4.5, 3.2, 3.3, 3.1, 3.4)
> se<-c(sqrt(data))
> bar.plot(data, se,ind.side="lo", ylim=c(0, 10), col="lavender", space=0.25,
> hat=0) 
> 
> data<-c(15, 15, 19, 22)
> se<-c(3, 5, 6, 4.5)
> nam<-c("L.c","O.v","C.i","L.u")
> bar.plot(data, se, col="orange", ind.side="bo", hat=0.05, main="",
> xlab="species", ylab="CT  conc.")
> 
> data<-c(4,5,1,1.3,6,7.1,5,2.6)
> se<-c(sqrt(data))
> nam<-rep(c("C","T"),4)
> spa<-rep(c(1.5, 0.2),4)
> col<-rep(c("green","red"),4)
> bar.plot(data, se, col=col, ind.side="up", main="Trees !", ylab="CT conc.",
> space=spa,  names.arg=nam)
> 
> 
> 
> 
> 
> ***********************************************************************
> Dieter Häring
> Eidg. Forschungsanstalt für Agraroekologie und Landbau (FAL)
> Reckenholzstrasse 191
> 8046 Zürich
> 
> Tel.       01 / 377 71 62
> FAX      01 / 377 72 01
> mailto:dieter.haering at fal.admin.ch
> www.reckenholz.ch
> 

Bar charts have many problems as pointed out in Bill Cleveland's book 
Elements of Graphing Data.  Bar charts with error bars have even more 
problems.  I prefer dot plots with error bars.  The Dotplot function in 
the Hmisc package will make such graphs.  Hmisc's xYplot will do 
likewise for line graphs, including an option for error bands and shaded 
error bands.

-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list