error.bars<-function (xv, z, nn){ xv<-barplot(yv, ylim=c(0,(max(yv)+max(z))), names=nn, ylab=deparse(substitute(yv))) g=(max(xv)-min(xv))/50 # the 50 determines the horizontal lengths of the bar at top and bottom of error bars for(I in 1 : length(xv)){ lines(c(xv[i],xv[i]), c(yv[i]+z[i],yv[i]-z[i])) #draws the vertical part of the line lines(c(xv[i]-g,xv[i]+g), c(yv[i]+z[i], yv[i]+z[i])) lines(c(xv[i]-g,xv[i]+g), c(yv[i]-z[i], yv[i]-z[i])) } # end I } #end function flies <-c("L. dux", "C. megacephala", "C. rufifacies", "C. nigripes", "L. cuprina", "M. domestica") males<-c(6032,4371,5367,4801,3678,3488) females<-c(6073,5635,5207,4778,3620,3427) standarddev_males<-c(385,344,385,323,155,110)*1.96 # data in the original paper are 1 S.D. standarddev_females<-c(207,514,244,277,209,231)*1.96 data<-matrix(c(males,females), 2, 6, byrow=TRUE) flies <-c("L. dux", "C. megacephala", "C. rufifacies", "C. nigripes", "L. cuprina", "M. domestica") males<-c(6032,4371,5367,4801,3678,3488) females<-c(6073,5635,5207,4778,3620,3427) standarddev_males<-c(385,344,385,323,155,110)*1.96 # data in the original paper are 1 S.D. standarddev_females<-c(207,514,244,277,209,231)*1.96 data<-matrix(c(males,females), 2, 6, byrow=TRUE) # the 2 and 6 are the number of rows and columns in the matrix respectively errors<-matrix(c(standarddev_males, standarddev_females), 2, 6, byrow=TRUE) bardata<-barplot(data,beside=TRUE, ylim=c(0,max(data)+max(errors)), ylab="Mean number ommatidea (+/- 95% confidence)", names=flies, cex.names=0.7, xlab="Fly species", col=c("lightblue","pink")) arrows(bardata,data+errors, bardata, data-errors, angle=90, code=3, length=0.1) legend("topright", fill=c("lightblue","pink"), c("males","females"))