Skip to content

How to add error bars to plot(x,y)in R?

7 messages · mingliz2@netscape.net, Dirk Eddelbuettel, Mark Myatt +4 more

#
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
#
"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
#
mingliz2 at netscape.net writes:
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Thu, 24 Jan 2002, Dirk Eddelbuettel wrote:

            
The real solution would be adding this feature as an option
to plot.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
Agustin Lobo wrote:
Maybe, but its very easy to add error bars to a plot. A nice example is
in 

http://cran.r-project.org/doc/contrib/rpsych.pdf

By, Sven
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
At 10:29 25/01/02 +0100, you wrote:

            
Respectfully I disagree as the conventions for 'error bars' differ in 
different branches of science and engineering.  Users are often/generally 
oblivious to the other conventions and will presume their preference is 
being implemented. Perhaps adding a 'script' directory to plot and 
incorporating the straightforward plotting function there might be a way to 
go, in this way users have to understand and to insert their own desired 
'confidence interval',


Richard Rowe
Senior Lecturer
Department of Zoology and Tropical Ecology, James Cook University
Townsville, Queensland 4811, Australia
fax (61)7 47 25 1570
phone (61)7 47 81 4851
e-mail: Richard.Rowe at jcu.edu.au
http://www.jcu.edu.au/school/tbiol/zoology/homepage.html

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._