错误:找到的对象不是统计信息

时间:2017-03-15 20:50:31

标签: r ggplot2

我使用以下代码:

library (ggplot2)
df=data.frame(score=c(1,3,5,9,7,8,4,1,2,6,1,6,2,1,3,1,3,5,8,4),
              age=c(2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3))  

ggplot(data=df,aes(x=age,y=score))+
     geom_point(position=position_jitter(width=.1),aes(color=age))+
     geom_line(stat = "hline", yintercept = "mean",aes(group=age))+
     stat_summary(geom = "line", fun.y="mean",aes(yend=..y..),width=.5)+
     stat_summary(fun.data="mean_cl_boot",geom="errorbar",width=.5)+
     theme_classic()

但是我收到了这个错误:

  

错误:找到的对象不是统计信息。

有谁知道如何解决这个问题?我之前用过这个,但现在它没有用。我想生产这样的东西:

enter image description here

我想有平均值但我只能生产这样的东西:

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用stat_summary使用错误栏geom并通过特殊变量yminymax..y..设置为摘要统计信息来获得相同的结果。

ggplot(df, aes(x = age, y = score)) + 
    geom_point(position = position_jitter(width = .2), aes(color = age)) +
    stat_summary(fun.data = "mean_cl_boot", geom = "errorbar", width = .5) +
    stat_summary(geom = "errorbar", fun.y = mean, aes(ymin = ..y.., ymax = ..y..))