将一段回归线添加到图

时间:2015-06-20 03:45:20

标签: r plot

我有4条回归线,我希望将它们放在一个图中。这是我的简单代码:

data = read.csv("TEST.csv", header = FALSE)
plot(data$V1,data$V2)
fit <- lm(data$V2~data$V1)
abline(fit, col=1)

data1 = read.csv("TEST1.csv", header = FALSE)
fit1 <- lm(data1$V2~data1$V1)
abline(fit1, col=2)

data2 = read.csv("TEST2.csv", header = FALSE)
fit2 <- lm(data2$V2~data2$V1)
abline(fit2, col=3)

data3 = read.csv("TEST3.csv", header = FALSE)
fit3 <- lm(data3$V2~data3$V1)
abline(fit3, col=4)

这是情节: Regression plot

但这不是我想要的。黑线很好,但对于红线,蓝线和绿线,我只想让这些线的一段接近数据点。例如,只是3到4之间的绿线段。数据集位于此处:

TEST.csv:https://www.dropbox.com/s/aphd5ts9hxlm2wj/TEST.csv?dl=0

TEST1.csv:https://www.dropbox.com/s/dp3diwu4tuynbjp/TEST1.csv?dl=0

TEST2.csv:https://www.dropbox.com/s/b6zr88ottf3wmjg/TEST2.csv?dl=0

TEST3.csv:https://www.dropbox.com/s/o0qc2987gb04g7m/TEST3.csv?dl=0

1 个答案:

答案 0 :(得分:3)

一种方法是使用clip

data = read.csv("TEST.csv", header = FALSE)
plot(data$V1,data$V2)
fit <- lm(data$V2~data$V1)
abline(fit, col=1)

data1 = read.csv("TEST1.csv", header = FALSE)
clip(min(data1$V1), max(data1$V1), min(data1$V2), max(data1$V2))
fit1 <- lm(data1$V2~data1$V1)
abline(fit1, col=2)

data2 = read.csv("TEST2.csv", header = FALSE)
clip(min(data2$V1), max(data2$V1), min(data2$V2), max(data2$V2))
fit2 <- lm(data2$V2~data2$V1)
abline(fit2, col=3)

data3 = read.csv("TEST3.csv", header = FALSE)
clip(min(data3$V1), max(data3$V1), min(data3$V2), max(data3$V2))
fit3 <- lm(data3$V2~data3$V1)
abline(fit3, col=4)

所以,我正在做的是将拟合的线图限制到它们各自的数据集范围。限制非常严格,但可以重新调整以获得超出其数据集范围的拟合线。