dieter.haering@fal.admin.ch
2004-Jun-18 14:13 UTC
[R] Barplots and error indicators: Some R-Code
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
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
Dear all,> -----Original Message----- > From: Frank E Harrell Jr [SMTP:f.harrell at vanderbilt.edu] > Sent: Friday, June 18, 2004 4:48 PM > To: dieter.haering at fal.admin.ch > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Barplots and error indicators: Some R-Code > > 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 >it might be a bit off-topic but can anyone suggest some online material concerning good graph / bad graph examples? I imagine something like: a) These are the data and this is the main feature of the data which should be represented. b) This is a bad idea how to represent a) c) This is a good idea how to represent a) I know there are excellent books by Cleveland and also by Tufte ("The Elements of Graphing Data", "The Visual Display of Quantitative Information", "The cognitive style of PowerPoint", ...) but it would be sometimes just more convenient to refer to some online material. Maybe even some online material about the problems of barplots with error bars? Thank you very much, Roland +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}