r - 错误: Found object is not a stat

标签 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()

但我收到此错误:

Error: Found object is not a stat.

有谁知道这个问题怎么解决吗?我以前用过这个,但现在不起作用。我想制作这样的东西:

enter image description here

我想要平均条,但我只能产生这样的东西:

enter image description here

最佳答案

您可以使用 stat_summary 与错误栏几何对象并通过特殊变量将 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..))

关于r - 错误: Found object is not a stat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42820438/

相关文章:

r - 向grid.table图添加文本

r - 箱线图 mustache 上的样本大小

r - 在一个数据框列中使用多个时区

r - ggplot2 错误 : object 'ratio' not found

r - 如何在 R 中的 data.frame 中聚合数据

r - 根据摘要统计过滤掉 data.table 列

r - 在 R 中绘制表达式树

r - 使用 geom_bar 更改填充的图例形状大小

r - 不使用表达式的条形图标签下标

r - 如何随机化 ggplot 因子配色方案,在色轮上保持相同的默认距离?