【问题标题】:Plot segment between point and line绘制点和线之间的线段
【发布时间】:2015-11-30 17:38:42
【问题描述】:

我有以下数据集:

x <- 1:5
y <- c(1, 2, 1.3, 3.75, 2.25)

而且我需要使用简单回归来绘制适合我的数据集的直线,以及以下几点:

plot(x, y, pch=19, ylim=c(0,6))
xx <- seq(0, 6, length=100)
fit <- lm(y~x)
lines(xx, predict(fit, data.frame(x=xx)))

现在我想将图中的点连接到直线,如下图(示例)所示,显示相关的预测误差:

我该怎么办?

【问题讨论】:

    标签: r plot line linear-regression


    【解决方案1】:

    使用 base r 您可以执行以下操作:

    x <- 1:5
    y <- c(1, 2, 1.3, 3.75, 2.25)
    
    fit <- lm(y ~ x)
    plot(x, y)
    abline(fit)
    res <- signif(residuals(fit), 5)
    pre <- predict(fit)
    segments(x, y, x, pre, col = rainbow(length(x)))
    

    calibrate::textxy 可以轻松添加标签:

    # install.packages("calibrate")
    library(calibrate)
    textxy(x, y, res)
    

    【讨论】:

      【解决方案2】:

      我喜欢 broom 包,它可以为这样的事情生成一个漂亮的数据框:

      library(broom)
      aug_fit = broom::augment(fit)
      
      with(aug_fit, segments(x0 = x, y0 = y, x1 = x, y1 = .fitted))
      

      在你的情节产生后运行我的with(... segments()) 行:

      我将把添加颜色、文本标签等的工作留给你。

      【讨论】:

        【解决方案3】:

        使用ggplot:

        library(ggplot2)
        
        #ggplot needs a dataframe
        dat <- data.frame(x=x,y=y)
        fit <- lm(y~x,data=dat)
        
        #add predicted y for each x, to enable segment drawing
        dat$pred <- predict(fit, dat)
        
        with thanks to JasonAizkalns: adding labels too
        dat$pred <- predict(fit, dat)
        dat$pred_error <- dat$y - dat$pred 
        dat$vjust <- sign(dat$pred_error)*-1.1 #labels can appear above/below points now
        
          p1 <- ggplot(dat, aes(x=x,y=y, color=factor(x)))+
          geom_point(size=2) + 
          geom_segment(aes(x=x,xend=x,y=y,yend=pred)) +
          geom_abline(intercept=coefficients(fit)[1],slope=coefficients(fit)[2]) +
          geom_text(aes(label=round(pred_error,2),vjust=vjust))+
          ylim(c(0,5))+
          xlim(c(0,5))+
          theme_bw()+
          theme(legend.position="none")
        p1
        

        【讨论】:

        • 我已经编辑了答案,谢谢。通常在 geom_text 中,我喜欢在 aes 中映射标签并设置始终相同的东西。
        【解决方案4】:

        另一个ggplot2 答案。,在精神上与Gregors 答案相似。这利用了fortify.lm,您可以将来自lm 回归的结果传递给ggplot。要查看 fortify 的作用,您可以查看对象 fortify(fit)

        # Your data and linear model
        x <- 1:5
        y <- c(1, 2, 1.3, 3.75, 2.25)
        fit <- lm(y~x)
        
        # Plot
        library(ggplot2)    
        
        ggplot(fit, aes(x=x, y=y, xend=x, yend=y, col=factor(x), label=round(.resid, 2))) +
             geom_point() +
             geom_line(aes(x=x, y=.fitted), inherit.aes=FALSE) +
             geom_segment(aes(y=.fitted)) +
             geom_text(aes(vjust=-1*sign(.resid))) +
             theme(legend.position="none")
        

        【讨论】:

          猜你喜欢
          • 2011-05-08
          • 2019-08-15
          • 2014-12-23
          • 2018-12-19
          • 2021-07-02
          • 2011-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多