[R] error bars in line plots

Ott Toomet siim at obs.ee
Mon Oct 7 09:23:08 CEST 2002

On 6 Oct 2002, Myriam Abramson wrote:

  |Could you tell me how I can draw a graph with error bars? 
  |Sorry, I don't use R that often and I couldn't find it easily in the

There is no errorbars function in the package.  But you may easily construct
your own using segments().  I add my own versions below (actually, why not
put something similar into the base package?).

You may use it as:
errorbars(x,y, dy=dy)

The second function is for using with matplot().



errorbars <- function(x, y, dy=NULL, dx=NULL, ...) {
  if(dy) {
    segments(x, y-dy, x, y+dy, ...)
  if(dx) {
    segments(x-dx, y, x+dx, y, ...)

materrorbars <- function(x, y, dy=NULL, dx=NULL,
                         col=par("col"), lty=par("lty")) {
  M <- ncol(y)
    N <- nrow(x)
  else {
    N <- length(x)
    x <- matrix(x, N, M)
  karv <- rep(col, length.out=M)
  joon <- rep(lty, length.out=M)
  if(!is.null(dy)) {
    for(i in seq(M)) {
      segments(x[,i], y[,i]-dy[,i], x[,i], y[,i]+dy[,i],
               col=karv[i], lty=joon[i])
  if(!is.null(dx)) {
    for(i in seq(M)) {
      segments(x[,i]-dx[,i], y[,i], x[,i]+dx[,i], y[,i],
               col=karv[i], lty=joon[i])

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