[R] ggplot2: problem with geom_errorbar and geom_abline

James D Forester jdforest at uchicago.edu
Wed Oct 3 19:12:10 CEST 2007


Hi all,

I  have run into what appears to be a bug in ggplot2; however, I am new 
to the ggplot syntax, so I might be missing a key element. The main 
issue is that I cannot get geom_abline to plot when colour is used to 
identify "group" in the main plot. When I remove colour, geom_abline 
works but the error bars have an undesirable line drawn between the left 
edges of the caps. Does anyone know how to change this behavior?

I am using R version 2.5.1 and ggplot2 version 0.5.5 on Ubuntu Feisty.

Thanks for your help.

James


## I am using an example from http://had.co.nz/ggplot2/geom_errorbar.html
library(ggplot2)
df <- data.frame(
     trt = factor(c(1, 1, 2, 2)),
     resp = c(1, 5, 3, 4),
     group = factor(c(1, 2, 1, 2)),
     se = c(0.1, 0.3, 0.3, 0.2))

## The following three lines work fine and produce the same graphic as 
seen on the example website
limits <- aes(max = resp + se, min=resp - se, width=0.9)
p <- ggplot(df, aes(colour=group, y=resp, x=trt))
p + geom_point() + geom_errorbar(limits, width=0.2)

##However, the following line fails with:
## 'Error in eval(expr, envir, enclos) : object "group" not found'
p + geom_point() + geom_errorbar(limits, width=0.2) 
+geom_abline(slope=0,intercept=3,linetype=2)

##If I remove colour from the plot, abline works but now there are black 
lines connecting the error bars
p <- ggplot(df, aes(y=resp, x=trt))
p + geom_point() + geom_errorbar(limits, width=0.2) 
+geom_abline(slope=0, intercept=3,linetype=2)



More information about the R-help mailing list