[R] adding SE to bar plots

Anne York
2002-11-25T17:27:14Z

This simple function was written to superimpose error bars on a scatter
plot but can be also used with simple barplots. See second example.

"superpose.eb" <- function (x, y, ebl, ebu = ebl, lh = 0.01,   ...)
{
#function to add error bars to an existing scatterplot
#written by Anne York, May 2002
#+++++++++++++++++++++++++++++++++++++++++++++++++++
#ebu is the length of the upper error bar; assumed to be a vector with same
#                                          length of x and y
#ebl is the length of the lower error bar; assumed to be a vector with same
#                                          length of x and y
#2*lh is a the length of the tick at the end of the error bar
#it is assumed that the limits of the axes on the existing plot are
#sufficiently large to accomodate the error bars.

    segments(x, y + ebu, x, y - ebl, ...)
    segments(x - lh , y + ebu, x + lh , y + ebu,
        ...)
    segments(x - lh, y - ebl, x + lh , y - ebl,  ...)
}



#Examples:
my.ebl <- 2*abs(rnorm(10))
plot(1:10,1:10,ylim = c((0-max(ebl)),10+max(ebl)))
superpose.eb(1:10,1:10,ebl=my.ebl, lh = 0.05)


#Also works with a simple barplot:
dum <- barplot(1:10,ylim=c(0,12))
my.ebu <- abs(rnorm(10))
superpose.eb(dum[,1],1:10,ebl = 0, ebu=my.ebu, lh = 0.1, lwd = 3, col
="red")
points(dum[,1],1:10,pch=16, col = "red") # covers up the lower tick
mark

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Date: Tue, 19 Nov 2002 15:28:56 -0800
From: Bill Shipley <Bill.Shipley at Usherbrooke.ca>
Subject: [R] adding SE to bar plots

Hello.  I know that one can add standard errors to a bar plot by repeated
use of the lines() function, but this can be long when there are many
bars.  I wonder if there is not already some other function that does this
automatically?

Bill Shipley
Departement de biologie
Universite de Sherbrooke
Sherbrooke (Quebec) CANADA J1K 2R9
Bill.Shipley at USherbrooke.ca
http://callisto.si.usherb.ca:8080/bshipley/



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anne E. York
National Marine Mammal Laboratory
Seattle WA 98115-0070  USA
e-mail: anne.york at noaa.gov
Voice: +1 206-526-4039
Fax: +1 206-526-6615
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~