在点和线之间绘制线段

时间:2021-04-12 23:44:58

I have the following dataset:

我有以下数据集:

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

And I need to draw the straight line that fits my dataset by using simple regression, as well as these points:

我需要用简单的回归画出符合我数据集的直线,以及这些点:

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)))

Now I would like to join the points in the plot to the line as in the following (example) picture, showing the related prediction error:

现在我想将图中的点与下面的(示例)图片中的线连接起来,显示相关的预测误差:

在点和线之间绘制线段

How can I do?

我怎么能做什么?

4 个解决方案

#1


2  

Using base r you could do the following:

使用基数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)))

在点和线之间绘制线段

Adding labels is easy with calibrate::textxy:

添加标签很容易校准::textxy:

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

在点和线之间绘制线段

#2


1  

I like the broom package for generating a nice data frame for things like this:

我喜欢用broom包来生成这样的数据框架:

library(broom)
aug_fit = broom::augment(fit)

with(aug_fit, segments(x0 = x, y0 = y, x1 = x, y1 = .fitted))

Running my with(... segments()) line after your plot yields:

跑步我(…分段()线后,您的地块产量:

在点和线之间绘制线段

I'll leave adding colors, text labels, etc. to you.

我会给你添加颜色,文字标签等。

#3


0  

Using ggplot:

使用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

在点和线之间绘制线段

#4


0  

Another ggplot2 answer., and similar in spirit to Gregors answer. This makes use of fortify.lm, where you can pass the results from anlm regression to ggplot. To see what fortify does, you can look at the object fortify(fit).

另一个ggplot2回答。在精神上与群居者的答案相似。这就利用了防御。lm,您可以将anlm回归的结果传递到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")

#1


2  

Using base r you could do the following:

使用基数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)))

在点和线之间绘制线段

Adding labels is easy with calibrate::textxy:

添加标签很容易校准::textxy:

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

在点和线之间绘制线段

#2


1  

I like the broom package for generating a nice data frame for things like this:

我喜欢用broom包来生成这样的数据框架:

library(broom)
aug_fit = broom::augment(fit)

with(aug_fit, segments(x0 = x, y0 = y, x1 = x, y1 = .fitted))

Running my with(... segments()) line after your plot yields:

跑步我(…分段()线后,您的地块产量:

在点和线之间绘制线段

I'll leave adding colors, text labels, etc. to you.

我会给你添加颜色,文字标签等。

#3


0  

Using ggplot:

使用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

在点和线之间绘制线段

#4


0  

Another ggplot2 answer., and similar in spirit to Gregors answer. This makes use of fortify.lm, where you can pass the results from anlm regression to ggplot. To see what fortify does, you can look at the object fortify(fit).

另一个ggplot2回答。在精神上与群居者的答案相似。这就利用了防御。lm,您可以将anlm回归的结果传递到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")