【问题标题】:Add Regression Line ggplot for Only Certain Groups仅为某些组添加回归线 ggplot
【发布时间】:2013-03-14 14:44:18
【问题描述】:

我想在我的 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) 

【问题讨论】:

    标签: r ggplot2 trendline


    【解决方案1】:

    一种解决方案是将您的数据的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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2021-05-21
      • 2018-08-28
      • 2021-10-25
      • 2016-02-14
      • 2021-01-14
      相关资源
      最近更新 更多