[R] Help with plotting error bars in R
ben@zoo.ufl.edu
ben at zoo.ufl.edu
Mon Apr 16 21:29:07 CEST 2001
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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list