[R] plot graph with error bars trouble

Ben Bolker bolker at zoo.ufl.edu
Sun Sep 30 22:05:32 CEST 2007

hadley wickham wrote:
> I'd do this a little differently, using the reshape
> (http://had.co.nz/reshape) and ggplot2 (http://had.co.nz/ggplot2)
> packages:
> library(reshape)
> library(ggplot2)
> # Get data in format required for reshape
> df <- rename(df, c("OD" = "value"))
> # Summarise and compute errors
> se <- function(x) sd(x)/sqrt(length(x))
> means <- cast(df, Mutant + Time ~ ., c(mean, se))
> qplot(Time, mean, data=means, colour=Mutant, min = mean - se, max =
> mean + se, geom=c("point","errorbar"))

  I can certainly see the value of a more elegant/automated/higher-level 
(although knowing the base-graphics way of going about it can be helpful 

   However, at least the first plot you suggest looks a little bit funny 
to me.
Suppose that the x coordinates of the error
bar are x_L (left edge of bars), x_0 (middle of bars/location
of data point), and x_R (right edge of bars), and the y coordinates
are y_B, y_0, and y_U (bottom [min], middle, and top [max]).
The "errorbar" specification seems to add a series of
segments from (x_L,y_B) of one point to
(x_L,y_U) of the next  [sorry if this is confusing, but I'm trying
to describe a picture ...]

(1) this seems like a funny default (I wouldn't normally want this line
drawn if I specified "errorbars"), (2) even if one has the line,
 the x-positions of the segment endpoints still seem wrong (or at least 
a wrong default) -- I still
think they should line up with the x-coordinates of the data

  Poking around http://had.co.nz/ggplot2/geom_errorbar.html hasn't 
resolved this (yet).

  I thought that breaking it down a bit might help, but it appears to do 
the same thing ...

p = ggplot(means, aes(x=Time, y=mean, 
p + geom_errorbar()

  Can you see what I mean, or am I confused/have an old version/etc. ?


 > sessionInfo()
R version 2.5.1 (2007-06-27)

LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] "splines"   "grid"      "stats"     "graphics"  "grDevices" 
"utils"     "datasets"  "methods"   "base"    

other attached packages:
     ggplot2   colorspace RColorBrewer         MASS        proto      
     "0.5.5"       "0.95"      "1.0-1"     "7.2-36"      "0.3-8"      


PS if one specifies "errorbars" without specifying min and max one gets 
the error

Error in rbind(max, max, max, min, min, min) :
        cannot coerce type closure to list vector

  perhaps a more transparent error message could be supplied in this 
stupid-user-error-obvious-in-hindsight) case?

More information about the R-help mailing list