代码之家  ›  专栏  ›  技术社区  ›  MAPK

使用ggplot将geom-abline添加到glm的残差与拟合数据中

  •  0
  • MAPK  · 技术社区  · 5 年前

    我的数据是这样的:

    y = c(12, 14, 33, 50, 67, 74, 123, 141, 165, 204, 253, 246, 240)
    t = 1:13
    aids.pois <- glm(y~t, data=data, family="poisson")
    cc <- data.frame(aids.pois$residuals, aids.pois$fitted.values)
    

    我想在下面的图中添加geom a bline,但无法显示abline:

    ggplot(cc, aes(x = aids.pois.fitted.values, y = aids.pois.residuals)) + 
      geom_point() +
      geom_abline()
    

    我期待着这样的事情(但使用ggplot)

    plot(aids.pois, which = 1, main = "base R: Residual Vs fitted plot")
    

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  1
  •   MrFlick    5 年前

    如果你想要类似的东西 plot() 正在做,你可以做这个

    cc <- data.frame(resid=resid(aids.pois), fitted=fitted(aids.pois))
    
    ggplot(cc, aes(x = fitted, y = resid)) + 
      geom_smooth(method="loess", color="red", se=FALSE) + 
      geom_hline(yintercept = 0, linetype=2, color="darkgrey") +
      geom_point()
    

    enter image description here

    红线来自 标号() 只是一个黄土平滑的绘制点。您可以使用 geom_hline . 不需要 geom_abline 在这里。