仅为某些组添加回归线ggplot

use*_*248 5 r ggplot2 trendline

我想在我的ggplot中添加趋势线,但仅限于重要的关系.现在geom_smoothstat_smooth为每个组添加趋势线,但我想指定哪些组获得趋势线而哪些不获得趋势线.

下面是我的脚本示例:

plot20<-ggplot(data, aes(x=data$Density, y=data$Total.degrees, color=Species, shape=Species)) 
+ geom_point(size=3) 
+ scale_shape_manual(values=shapeset) 
+ scale_colour_manual(values=colorset) 
+ theme(legend.position="none") 
+ geom_smooth(method=lm, se=FALSE) 
Run Code Online (Sandbox Code Playgroud)

Did*_*rts 12

一种解决方案是将subset()您的数据放入其中geom_smooth()并为您绘制趋势线所需的值.

作为示例使用的数据mtcars(未提供样本数据).用subset() cyl的4或6的值被选择.Insede geom_smooth()aes()应该重复.

ggplot(mtcars,aes(wt,mpg,color=factor(cyl)))+geom_point()+
    geom_smooth(data=subset(mtcars,cyl==4 | cyl==6),
               aes(wt,mpg,color=factor(cyl)),method=lm,se=FALSE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述