Dear R Experts, Is there a way in R to add an error bar (say in the y direction) for each data point? Thanks Ming Chow -- __________________________________________________________________ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Ming" == mingliz2 <mingliz2 at netscape.net> writes:
Ming> Dear R Experts, Is there a way in R to add an error bar (say in the
Ming> y direction) for each data point?
This is a FAQ. Here is but one reply from the mailing list.
Dirk
To: Baruch P Feldman <baruchf at mit.edu>
cc: <r-help at stat.math.ethz.ch>
Subject: Re: [R] Help with plotting error bars in R
Date: Mon, 16 Apr 2001 15:29:07 -0400 (EDT)
Status: O
Content-Length: 4307
Lines: 108
This comes up as a FAQ every so often (it hasn't yet been added to the
official FAQ list), you can find it in the mail archives in several
places.
http://www.R-project.org/nocvs/mail/r-help/2000/0451.html
http://www.R-project.org/nocvs/mail/r-help/2000/2289.html
http://www.R-project.org/nocvs/mail/r-help/2000/3326.html
(plus a whole recent discussion of whether to include a plotCI function
in the R base, or just to tell people how to do what they want using
arrows() or segments())
Here's the most recent version of my function:
plotCI <- function (x, y = NULL, uiw, liw = uiw, aui=NULL, ali=aui,
err="y", ylim=NULL, sfrac = 0.01, gap=0,
add=FALSE,
col=par("col"), lwd=par("lwd"),
slty=par("lty"), xlab=NULL,
ylab=NULL, ...) {
## originally from Bill Venables, R-list
if (is.list(x)) {
y <- x$y
x <- x$x
}
if (is.null(y)) {
if (is.null(x))
stop("both x and y NULL")
y <- as.numeric(x)
x <- seq(along = x)
}
if (missing(xlab)) xlab <- deparse(substitute(x))
if (missing(ylab)) ylab <- deparse(substitute(y))
if (missing(uiw)) { ## absolute limits
ui <- aui
li <- ali
}
else { ## relative limits
if (err=="y") z <- y else z <- x
ui <- z + uiw
li <- z - liw
}
if (is.null(ylim)) ylim <- range(c(y, ui, li), na.rm=TRUE)
if (add) {
points(x, y, col=col, lwd=lwd, ...)
} else {
plot(x, y, ylim = ylim, col=col, lwd=lwd, xlab=xlab, ylab=ylab, ...)
}
if (gap==TRUE) gap <- 0.01 ## default gap size
ul <- c(li, ui)
if (err=="y") {
gap <- rep(gap,length(x))*diff(par("usr")[3:4])
# smidge <- diff(par("usr")[1:2]) * sfrac
smidge <- par("fin")[1] * sfrac
# segments(x , li, x, pmax(y-gap,li), col=col, lwd=lwd, lty=slty)
# segments(x , ui, x, pmin(y+gap,ui), col=col, lwd=lwd, lty=slty)
arrows(x , li, x, pmax(y-gap,li), col=col, lwd=lwd, lty=slty, angle=90,
length=smidge, code=1)
arrows(x , ui, x, pmin(y+gap,ui), col=col, lwd=lwd, lty=slty, angle=90,
length=smidge, code=1)
## horizontal segments
# x2 <- c(x, x)
# segments(x2 - smidge, ul, x2 + smidge, ul, col=col, lwd=lwd)
}
else if (err=="x") {
gap <- rep(gap,length(x))*diff(par("usr")[1:2])
smidge <- par("fin")[2] * sfrac
# smidge <- diff(par("usr")[3:4]) * sfrac
arrows(li, y, pmax(x-gap,li), y, col=col, lwd=lwd, lty=slty, angle=90,
length=smidge, code=1)
arrows(ui, y, pmin(x+gap,ui), y, col=col, lwd=lwd, lty=slty, angle=90,
length=smidge, code=1)
## vertical segments
# y2 <- c(y, y)
# segments(ul, y2 - smidge, ul, y2 + smidge, col=col, lwd=lwd)
}
invisible(list(x = x, y = y))
}
On Mon, 16 Apr 2001, Baruch P Feldman wrote:
> Hi,
>
> I'm sorry to send email to everyone on this list, but I have a
simple question
> which is bothering me and I can't seem to figure out the correct
answer. I
> just downloaded R and I'm trying to reproduce some simple analysis
I've done
> on other packages. In particular, if I have a vector of experimental
values
> and a vector of their uncertainties, is there a way to plot the values
using
> the uncertainties as error bars? Other than this one problem R so far
seems to
> be a very good package.
>
> I'd greatly appreciate it if someone could reply to my email
directly.
>
> Thank you.
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
318 Carr Hall bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
--
Good judgment comes from experience; experience comes from bad judgment.
-- F. Brooks
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
mingliz2 at netscape.net writes:>Dear R Experts, > >Is there a way in R to add an error bar (say in the y direction) for each data >point?Have a look at my notes on R from: http://www.myatt.demon.co.uk Mark -- Mark Myatt -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Here is a very simple function to superimpose error bars (in the
y-direction) on an existing plot. Play with lh to get desired width. You
have to take care that the plot limits are wide enough to accomodate the
error bars.
"superpose.eb" <-
function(x,y,ebl,ebu = ebl,lh=.01,...){
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,
...)
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Thu, 24 Jan 2002 mingliz2 at netscape.net wrote:
>Dear R Experts,
>
>Is there a way in R to add an error bar (say in the y direction) for each
data point?
>
>Thanks
>
>Ming Chow
>--
>
>
>
>
>__________________________________________________________________
>
>
>
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
The package gregmisc contains a function plotCI which handles this nicely.> -----Original Message----- > From: mingliz2 at netscape.net [mailto:mingliz2 at netscape.net] > Sent: Thursday, January 24, 2002 8:21 PM > To: r-help at stat.math.ethz.ch > Subject: [R] How to add error bars to plot(x,y)in R? > > > Dear R Experts, > > Is there a way in R to add an error bar (say in the y > direction) for each data point? > > Thanks > > Ming Chow > -- > > > > > __________________________________________________________________ > > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.- > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: > r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._._._ >LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._