Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Found object is not a stat

Tags:

r

ggplot2

I'm using the following code:

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

But I get this error:

Error: Found object is not a stat.

Does anyone know how to solve this problem? I used this before but now it's not working.I want to produce something like this:

enter image description here

I would like to have the mean bar but I could only produce something like this:

enter image description here

like image 209
Sérgio Ferreira Cardoso Avatar asked Nov 29 '25 16:11

Sérgio Ferreira Cardoso


1 Answers

You can achieve the same result using stat_summary with the errorbar geom and setting ymin and ymax to be the summary statistic via the special variable ..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..))
like image 155
aosmith Avatar answered Dec 02 '25 04:12

aosmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!