[R] Error bars on barplots

KAREN KOTSCHY karen at gecko.biol.wits.ac.za
Tue Sep 7 12:24:38 CEST 1999


Hi everyone

I've finally had a chance to look at this problem again. Thanks to 
Paul Murrell and Bendix Carstensen, putting error bars on barplots 
with single bars is quite simple, as barplot returns the x locations 
of the bar midpoints.

However... with plots where you have groups of bars (where x is a 
matrix and beside=T), it is not as simple. One can work out the x 
locations of each individual bar manually, knowing the bar width 
and the midpoint of the group. I've written a function to do this when 
there are 3 bars in a group (see below). I'm sure the R gurus out 
there could suggest a more elegant or a simpler way!

Here are my functions, for anyone else wanting to put error bars 
onto these kind of barplots. Hopefully someone might find them 
useful.

# This calculates the x locations of individual bars, where there are 
# 3 bars per group.
# Arguments: x = the vector of bar group midpoints returned by 
# barplot.

barxloc <- function(x) {
   j <- 1
   v <- c()
   for (i in 1:length(x)) {
      v[[j]] <- x[[i]] - 1
      j <- j + 1
      v[[j]] <- x[[i]]
      j <- j + 1
      v[[j]] <- x[[i]] + 1
      j <- j + 1
      }
   return(v)
}

# This adds error bars to the plot (y+se only):
# Arguments: x = as above, means and se = matrices containing 
# the data to be plotted.

errbars <- function(x, means, se) {
   a <- barxloc(x)
   a <- unlist(a)
   arrows(a, means, a means+se, angle=90, code=2, length=0.1)
}

----------------------------

Karen Kotschy
Centre for Water in the Environment
University of the Witwatersrand
Johannesburg

Tel: 011 716-2218
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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