[R] plot graph with error bars trouble

hadley wickham h.wickham at gmail.com
Sun Sep 30 22:40:23 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
> solution
> (although knowing the base-graphics way of going about it can be helpful
> too).
>
>    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,
> colour=Mutant,min=mean-se,max=mean+se))
> p + geom_errorbar()
>
>   Can you see what I mean, or am I confused/have an old version/etc. ?
>

Oooooh - oops.  I completely forgot about that error - it's fixed in
the current development version of ggplot which I should probably
think about uploading to CRAN.

> 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
> (admittedly
> stupid-user-error-obvious-in-hindsight) case?

Yes, that's a good idea.  I'm still working on making the error
messages more user friendly.  I think I'm making some progress, but
it's fairly slow.

Hadley


-- 
http://had.co.nz/



More information about the R-help mailing list