geom_smooth on a subset of data

后端 未结 2 855
半阙折子戏
半阙折子戏 2020-12-20 13:34

Here is some data and a plot:

set.seed(18)
data = data.frame(y=c(rep(0:1,3),rnorm(18,mean=0.5,sd=0.1)),colour=rep(1:2,12),x=rep(1:4,each=6))

ggplot(data,aes         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 13:42

    It's as simple as geom_smooth(data=subset(data, x >= 2), ...). It's not important if this plot is just for yourself, but realize that something like this would be misleading to others if you don't include a mention of how the regression was performed. I'd recommend changing transparency of the points excluded.

    ggplot(data,aes(x=x,y=y,colour=factor(colour)))+
    geom_point(data=subset(data, x >= 2)) + geom_point(data=subset(data, x < 2), alpha=.2) +
    geom_smooth(data=subset(data, x >= 2), method='lm',formula=y~x,se=F)
    

    enter image description here

提交回复
热议问题