I’m trying to create a line graph and keep getting an error when I add in the error bars (just getting started with R, so apologies!). I’m not sure why – help would be appreciated!
Group = c("a","a","b","b","a","a","b","b")
Time = c(1,2,1,2,1,2,1,2)
Code = c("A","A","A","A","B","B","B","B")
Mean = (2,6,7,5,6,1,2,8)
SE = c(1.9,1.7,1.5,1.3,2,1.8,2.3,1.5)
dataset=data.frame(Group,Time,Code,Mean,SE)
ggplot(data=dataset) + geom_line(aes(x=Time,y=Mean,colour=Code,linetype=Group))+
scale_x_continuous(breaks=c(1,2)) +
scale_linetype_manual(values=c(2,1)) +
geom_point(aes(x=Time,y=Mean,colour=Code,linetype=Group)) +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE),width=.1,position=dodge)
The problem has to do with the last line — code works fine without it. But with it, I get: Error in eval(expr, envir, enclos) : object 'x' not found.
So what am I doing wrong with the geom_errorbar line?
The first thing I would try is to define the aesthetics only once and do so in the
ggplot()function. Ie.This is because
ggplotdoesn’t guarantee to pass all of the variables that are in the original dataset and weird results can result from depending on this.Edit: I just noticed that
xnever gets defined forgeom_errorbar, addingx=Timeto either theaesofggplot()orgeom_errorbar()should fix the problem. However, doing the latter is really not recommended.If you give example data (eg.
dput) I would be able to help you further.