I wrote this code:
df = data.frame(a=c('A','A','B','B'),b=c('X','Y','X','Y'),c=c(1,2,3,2))
q = ggplot(df, aes(a, y=c,fill=b))+
geom_bar(position=position_dodge(), stat="identity")
path = data.frame(x=c(1,2),y=c(2,2))
q = q + geom_path(data=path,aes(x=x, y=y))
I want to get a plot that have a horizontal line that start at the junction of the first two bar and end at the junction of the last two bar at height(position in Y axis)2.
However,my code give me an error:
"Error in eval(expr, envir, enclos) : object 'b' not found"
update: Thanks to Chris, moving the "fill" to geom_bar solves the orignal problem. But now, a new problem happens. I changed my code to:
library(ggplot2)
df=data.frame(a=c('A','A','B','B'),b=c('X','Y','X','Y'),c=c(1,2,3,2),err=c(.1,.2,.1,.2))
q = ggplot(df, aes(a, y=c))+
geom_bar(aes(fill=b),position=position_dodge(), stat="identity")+
geom_errorbar(aes(ymin=c-err,ymax=c+err), width=0.3, lwd = 1, position=position_dodge(0.9))
path = data.frame(x=c(1,2),y=c(2,2))
q = q + geom_path(data=path,aes(x=x, y=y))
print(q)
Because the "fill" is not in the original ggplot() function, the position of the error bar is messed up.