【问题标题】:Regression line for the entire data set together with regression lines based on groups整个数据集的回归线以及基于组的回归线
【发布时间】:2012-10-07 07:26:36
【问题描述】:

我是 ggplot2 的新手,在显示整个数据集的回归线以及每个组的回归线时遇到问题。

到目前为止,我可以根据组绘制回归线,但我没有成功在同一图上获得整个数据集的回归线。

我想要所有具有不同线条样式的回归线,以便在黑白打印中轻松识别它们。

到目前为止,这是我的代码:

ggplot(alldata, aes(y = y, x = x, colour= group, shape = group )) +
  geom_point(size = 3, alpha = .8) + 
  geom_smooth(method = "lm", fill = NA , size = 1) 

【问题讨论】:

    标签: r ggplot2 line regression


    【解决方案1】:

    尝试在ggplot2的原始调用中放置颜色、形状、线型美学

    然后你可以用不同的颜色添加整条线

    set.seed(1)
    library(plyr)
    alldata <- ddply(data.frame(group = letters[1:5], x = rnorm(50)), 'group', 
                     mutate, y=runif(1,-1,1) * x +rnorm(10))
    
    
    ggplot(alldata,aes(y = y, x = x)) +
      geom_point(aes(colour = group, shape = group), size = 3, alpha = .8) + 
      geom_smooth(method = "lm", se = FALSE, size = 1, 
                  aes(linetype = group, group = group)) +
      geom_smooth(method = "lm", size = 1, colour = 'black', se = F) +
      theme_bw()
    

    【讨论】:

    • @mnel 非常感谢!这就像魅力一样。我正在努力将主要回归线放入图例中,当我能够这样做时,将更新我的 cmets。
    猜你喜欢
    • 2021-04-28
    • 2023-03-13
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2016-05-18
    相关资源
    最近更新 更多