Error bars in plots
A common irritation in making plots with R is the difficulty one has with superimposing error bars, or more precise, confidence intervals (c.i.'s) on plots. It can be actually quite simple to do, although you keep forgetting how if you don't often use it (like I do). It is most convenient to define the function superpose.eb which accepts the x and y coordinates of the points around which the confidence intervals should be marked, and a lower bound and upper bound which indicate how much lower/higher than y the lower/higher extreme of the c.i. reaches. By default a symmetric c.i. is assumed, so that the upper bound parameter is optional:
function (x, y, ebl, ebu = ebl, length = 0.08, ...)
arrows(x, y + ebu, x, y - ebl, angle = 90, code = 3,
length = length, ...)
Its use is demonstrated in a barplot example (the RT data were kindly provided by Mariëtte Huizinga):
colnames(RT) = c("7", "11", "15", "21")
rownames(RT) = c("repetition", "alternation")
eblb = matrix(c(14,21,12,18,12,18,13,19),2,4) # 1.96 * s.d. of data
x.abscis <- barplot(RT, beside=TRUE, col=0:1, ylim=c(0,1200),
main="RT as a function of Age with 95%-confidance bars",
xlab="Age (yrs)")
superpose.eb(x.abscis, RT, eblb, col="orange", lwd=2)
barplot returns the abscissa at which the bars are plotted, and these are used as the x coordinates passed to superpose.eb. The result lookes like this:
Farrel Buchinsky said... How does your function definition compare to the built in function, "errbar"
The function errbar is not realy built-in; its a function from the Hmisc package... I don't know all the differences, but one difference appears to be that in errbar you always have to fully compute the confidence interval by yourself, as in
set.seed(1)
x <- 1:10
y <- x + rnorm(10)
delta <- runif(10)
errbar( x, y, y + delta, y - delta ) # you have to add and subtract delta from y by yourself
while superpose.eb can be called providing only means and associated s.d.'s (for asymptotic 68% c.i.'s), which is more intuitive for the people that ask me "how-to?" things like this (they apparently find it hard to think about how to calculate the locations for the lines to be drawn -- don't ask me why!). A more important difference is that errbar generates a plot if you do not set the add=TRUE parameter. Furthermore, superpose.eb has much much much less code than errbar (even if you would strip the optional plotting). Last but not least: setting col="orange" actually works in superpose.eb, as opposed to errbar.
14 Comments:
Thank you for this. I am trying to use a complicated tool (R) for simple tasks and I found this tip to be extremely useful.
How does your function definition compare to the built in function, "errbar"
Very useful! Thanks
Indeed very useful! Thnx!
Thnx! so useful...
A stroke of genius!
Thanks for saving me hours of frustration.
3 years later, and still helping people - thanks very much!
3 years and some months later, and still helping people - thank you so much much!
The information found here:
http://egret.psychol.cam.ac.uk/statistics/R/graphs1.html
are also of high value (for those who need more).
Reagards from Germany
Thanks for this, I use it all the time!
Thanks, that is actually really helpful. Saved me a lot of time!
J.
Almost 4 years later, and still helping people -- thank you very much!!!
I also found this extremely useful. I figured out how to plot error bars in minutes, rather than hours, thanks to this blog post.
Thank you. This is the best way I've found to plot error bars in R!
thank you! this is awesome!
Post a Comment
<< Home