I need to plot two error-bars on each point in a scatterplot. The usual is vertical error-bars that corresponds to the error on the points y-value, but I need to add the error-bar associated with the X-axis (horizontal) as well. I could probably do this with some abline command, but thought there might be a more clever way to do it with ggplot2?
1 Answer
Just for completion's sake, following up on my comment, here is a simply (albeit ugly) example:
df <- data.frame(x = 1:10,
y = 1:10,
ymin = (1:10) - runif(10),
ymax = (1:10) + runif(10),
xmin = (1:10) - runif(10),
xmax = (1:10) + runif(10))
ggplot(data = df,aes(x = x,y = y)) +
geom_point() +
geom_errorbar(aes(ymin = ymin,ymax = ymax)) +
geom_errorbarh(aes(xmin = xmin,xmax = xmax))

3 Comments
Jens Nielsen
Thanks a lot for that reply! it took me some time to reproduce your results with my own data as in my data the columns are NOT named "x" and "y", which (apparently) means that for the geom_errorbar you need to pass the x coordinate, that is: geom_errorbar(aes(x=var, ymin=...)) and for the geom_errorbarh both x and y, so: geom_errorbarh(aes(x=var1, y=var2, xmin=...)). This last detail of the horizontal geom_errorbarh does not seem to be documented in the help file, I had to deduce that from the error message I got.
Jens Nielsen
Sorry, I see that you define x and y in the first call to ggplot, that is what I should have done. Thanks again.
horseshoe
for those of you interested in it: you can change the width of the end of the errorbar and the color using: geom_errorbar(aes(ymin = ymin,ymax = ymax),color="red",width=0.1 )
geom_errorbarhthat takesx,xminandxmaxanalogously togeom_errorbar.