1

I cannot find my answer anywhere. I have two regression lines from two different datasets. I am trying to put these two regression lines in one graph. The following worked well.

regression1<-lm(Y ~ X, data = mydata1) 
regression2<-lm(Y ~ X, data = mydata2)
abline(regression1)
abline(regression2)

However in this plot I just have lines and I don't have dots. I run:

regression1<-lm(Y ~ X, data = mydata1)
regression2<-lm(Y ~ X, data = mydata2)
plot(c(0,2),c(0,2),type="n") +
points(rnorm(200), rnorm(200), col = "red") 
abline(regression1) 
abline(regression2)

With this command, I had just dots and I don't have lines, still does not work for me. What I want is having one graph with two different lines (representing the each regression's fitted lines) and dots of these regressions. I want these with different colours. Any help would be appreciated. Thanks

ASD
  • 13
  • 4
  • Can you post sample data? Please edit **the question** with the output of `dput(mydata1)`. Or, if it is too big with the output of `dput(head(mydata1, 20))`. And the same with `mydata2`. – Rui Barradas Jan 20 '19 at 15:23
  • I am sorry I cannot share the data, could you give an example from datasets from the R? – ASD Jan 20 '19 at 15:25
  • Check this https://stackoverflow.com/questions/51577271/add-two-linear-regression-lines-and-two-y-axis-in-ggplots – NelsonGon Jan 20 '19 at 15:26
  • With me the problem, with a fake dataset, was to open the graphics device with `plot(c(0,2), c(0,2), etc)`. The regression lines were *outside* the plotting area. I solved this problem with `with(mydata1, plot(range(X), range(Y), type = "n")) `. – Rui Barradas Jan 20 '19 at 15:29
  • `reg1<-lm(Y ~ X, data = mydata1) reg2<-lm(Y ~ X, data = mydata2) with(mydata1, plot(range(X), range(Y), type = "n"))+ with(mydata2, plot(range(X, range(Y), type = "n")) + abline(reg1) abline(reg2)` This does not work @RuiBarradas – ASD Jan 20 '19 at 15:38
  • See the answer. – Rui Barradas Jan 20 '19 at 15:42

1 Answers1

1

When, using base R graphics, you plot something and it doesn't show up in the graph the odds are that you have plotted it outside the plot area. In the example below I will make sure that everything is in the plot area by first getting appropriate x and y axis limits.

The first two code lines do the trick.

rangeX <- range(c(mydata1$X, mydata2$X))
rangeY <- range(c(mydata1$Y, mydata2$Y))

regression1 <- lm(Y ~ X, data = mydata1)
regression2 <- lm(Y ~ X, data = mydata2)
plot(rangeX, rangeY, type = "n", xlab = "X", ylab = "Y")
with(mydata1, points(X, Y, col = "red"))
with(mydata2, points(X, Y, col = "blue"))
abline(regression1, col = "red")
abline(regression2, col = "blue")

enter image description here

Data creation code.

set.seed(1234)

n <- 20
x <- seq_len(n) + rnorm(n)
mydata1 <- data.frame(X = x, Y = x + rnorm(n))
x <- seq_len(n) + rnorm(n)
mu <- 3
mydata2 <- data.frame(X = x + rnorm(n), Y = mu + x + rnorm(n))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • I wanted to ad the median line to the graph but it does not show in the line with the following. What might be the problem @RuiBarradas `rangeX <- range(c(mydata1$X, mydata2$X)) rangeY <- range(c(mydata1$Y, mydata2$Y)) regression1 <- lm(Y ~ X, data = mydata1) regression2 <- lm(Y ~ X, data = mydata2) plot(rangeX, rangeY, type = "n", xlab = "X", ylab = "Y") with(mydata1, points(X, Y, col = "red")) with(mydata2, points(X, Y, col = "blue")) abline(h = median(mydata1$X), col="black", lwd=3, lty=2) abline(regression1, col = "red") abline(regression2, col = "blue")` – ASD Jan 21 '19 at 11:41