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?
Asked
Active
Viewed 3.2k times
35
-
2I believe there's a `geom_errorbarh` that takes `x`, `xmin` and `xmax` analogously to `geom_errorbar`. – joran Feb 10 '12 at 17:08
-
http://permalink.gmane.org/gmane.comp.lang.r.ggplot2/3231 – Ben Bolker Feb 10 '12 at 17:09
1 Answers
45
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))

joran
- 169,992
- 32
- 429
- 468
-
4Thanks 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 Feb 13 '12 at 10:46
-
1Sorry, I see that you define x and y in the first call to ggplot, that is what I should have done. Thanks again. – Jens Nielsen Feb 13 '12 at 10:50
-
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 ) – horseshoe Jan 29 '19 at 14:31