【问题标题】:How to color ggplot points above and below a regression line in r?如何在r中为回归线上方和下方的ggplot点着色?
【发布时间】:2020-10-16 06:16:32
【问题描述】:

我有一个数据集和如下图:

rowid <- (1:100)
intercept <- (200:299)
df <- data.frame(rowid, intercept)

ggplot(df, aes(x=rowid, y=intercept, color=intercept)) + geom_point() 
+ scale_colour_gradient2(low="red", high="darkgreen", mid = "yellow", midpoint = 250) 
+ geom_smooth(method='lm', color = 'black')

我的数据集和绘图有更多的数据,所以它看起来像:

问题:

如何让回归线上方的点为某种颜色(绿色),回归线下方的点为某种颜色(红色),以及所有其他点为某种颜色(黄色)?

除此之外(如果可能),我想添加另一个列标签Grade,显示回归线上方的点列为“A”,所有其他点列为“C”,回归线下方列为“F”。

【问题讨论】:

    标签: r ggplot2 plot regression


    【解决方案1】:

    作为第一个示例(点不可能直接位于回归线上):计算每个 x 值的拟合值,在实际绘图之前拟合线性模型。之后,您可以简单地将布尔列绑定到数据框,指定预测值是小于还是大于真实 y。

    library(ggplot2)
    
    rowid <- (1:1000)
    intercept <- rnorm(1000, 0, 1)*rowid+1:1000
    mod <- lm(intercept ~ rowid)
    fitted <- mod$fitted.values
    smaller <- intercept < fitted
    df <- data.frame(rowid, intercept, smaller)
    
    
    ggplot(df, aes(x=rowid, y=intercept, color=as.factor(smaller))) + 
      geom_point()+
      geom_smooth(method='lm', color = 'black')+
      scale_color_manual(values = c("green", "yellow"))
    

    现在才阅读了您帖子的最后一句话,一种引入具有“A”、“F”和“C”级别的三级因子列并在之后相应地绘制它们的方法。我介绍了 200 个点,其中拟合值和真 y 相等:

    library(ggplot2)
    
    rowid <- (1:1000)
    intercept <- rnorm(1000, 0, 1)*rowid+1:1000
    mod <- lm(intercept ~ rowid)
    fitted <- mod$fitted.values
    #demonstration: add some points, where fitted value equals the true value
    sample_points <- sample(1:1000, 200, replace = FALSE)
    
    intercept[sample_points] <- fitted[sample_points]
    
    #construct three level factor, with a chain of ifelse:
    grade <- as.factor(ifelse(intercept < fitted, "F", ifelse(intercept == fitted, "C", "A")))
    df <- data.frame(rowid, intercept, grade)
    
    ggplot(df, aes(x=rowid, y=intercept, color=grade)) + 
      geom_point()+
      geom_smooth(method='lm', color = 'black')+
      scale_color_manual(values = c("green", "yellow", "red"))
    

    【讨论】:

    • 是否可以将回归线以上的所有点保存在新的数据框中?
    • 好吧,显然您可以根据我们之前创建的布尔值smaller 进行过滤,并将结果保存到单独的数据帧中。 df_smaller &lt;- df[smaller, ]df_bigger &lt;- df[!smaller, ] .
    【解决方案2】:

    你会想要重新实现 geom_smooth() 正在做的事情,比如

    m = lm(intercept~rowid, data=df) # note that format is y~x
    

    这将为您提供m$coefficients 下的斜率和截距。你可以检查一下

    ggplot(df, aes(x=rowid, y=intercept, color=intercept)) + geom_point() +
        scale_colour_gradient2(low="red", high="darkgreen", mid = "yellow", midpoint = 250) +
        # geom_smooth(method='lm', color = 'black') + 
        geom_abline(slope = m$coefficients[2], intercept = m$coefficients[1])
    

    替换geom_smooth()

    然后您可以在df 中创建一个新列,以告知您是在该行的上方/下方,还是只使用m$residuals。前一个选项的示例类似于

    df = cbind(df, up_down = if_else(
        df$intercept > df$row_id*m$coefficients[2]+m$coefficients[1],
        "above", 
        "below")
    ) # pseudocode
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2011-11-24
      相关资源
      最近更新 更多