Reputation: 5263
I have 4 regression lines and I want to have them in one plot. Here is my simple code:
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)
and here is the plot:
but it is not what I want to have. The black line is fine, but for red, blue and green lines, I just want to have a segment of those lines close to the data points. For example, just a segment of green line between 3 and 4. Datasets are located here :
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
Upvotes: 3
Views: 872
Reputation: 1281
One way to do it would be to use 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)
So, what I'm doing is limiting the fitted line-plots to their respective data-set ranges. The limits are extremely tight, but can be rescaled to get fitted lines that extend a bit outside their data-set ranges.
Upvotes: 3