带有ggplot的错误栏 - 获取错误

4 r standard-error ggplot2

我正在尝试创建一个折线图,并在我添加错误栏时继续收到错误(刚刚开始使用R,所以道歉!).我不确定为什么 - 帮助将不胜感激!

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)
Run Code Online (Sandbox Code Playgroud)

问题与最后一行有关 - 没有它,代码工作正常.但有了它,我得到:Error in eval(expr, envir, enclos) : object 'x' not found.

那我该怎么做错了geom_errorbar

Jon*_*han 5

我要尝试的第一件事是只定义一次美学,并在ggplot()函数中这样做.IE浏览器.

ggplot(data=dataset,aes(x=Time,y=Mean,colour=Code,linetype=Group,ymin=Mean-SE,ymax=Mean+SE)) 
+ geom_line()
+ scale_x_continuous(breaks=c(1,2))
+ scale_linetype_manual(values=c(2,1))
+ geom_point()
+ geom_errorbar(width=.1,position='dodge')
Run Code Online (Sandbox Code Playgroud)

这是因为ggplot不保证传递原始数据集中的所有变量,并且取决于此可能导致奇怪的结果.

编辑:我只注意到x从未获取定义geom_errorbar,添加x=Time到任何aesggplot()还是geom_errorbar()应该解决这个问题.但是,真的不推荐做后者.

如果您提供示例数据(例如dput),我将能够进一步帮助您.