Adding two regression graphs having problem in dots and lines












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










share|improve this question























  • 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 at 15:23











  • I am sorry I cannot share the data, could you give an example from datasets from the R?

    – ASD
    Jan 20 at 15:25











  • Check this stackoverflow.com/questions/51577271/…

    – NelsonGon
    Jan 20 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 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 at 15:38
















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










share|improve this question























  • 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 at 15:23











  • I am sorry I cannot share the data, could you give an example from datasets from the R?

    – ASD
    Jan 20 at 15:25











  • Check this stackoverflow.com/questions/51577271/…

    – NelsonGon
    Jan 20 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 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 at 15:38














1












1








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










share|improve this question














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







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 15:15









ASDASD

23




23













  • 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 at 15:23











  • I am sorry I cannot share the data, could you give an example from datasets from the R?

    – ASD
    Jan 20 at 15:25











  • Check this stackoverflow.com/questions/51577271/…

    – NelsonGon
    Jan 20 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 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 at 15:38



















  • 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 at 15:23











  • I am sorry I cannot share the data, could you give an example from datasets from the R?

    – ASD
    Jan 20 at 15:25











  • Check this stackoverflow.com/questions/51577271/…

    – NelsonGon
    Jan 20 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 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 at 15:38

















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 at 15:23





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 at 15:23













I am sorry I cannot share the data, could you give an example from datasets from the R?

– ASD
Jan 20 at 15:25





I am sorry I cannot share the data, could you give an example from datasets from the R?

– ASD
Jan 20 at 15:25













Check this stackoverflow.com/questions/51577271/…

– NelsonGon
Jan 20 at 15:26







Check this stackoverflow.com/questions/51577271/…

– NelsonGon
Jan 20 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 at 15:29





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 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 at 15:38





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 at 15:38












1 Answer
1






active

oldest

votes


















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





share|improve this answer
























  • That worked very well. Thanks.

    – ASD
    Jan 20 at 16:34











  • 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 at 11:41













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277852%2fadding-two-regression-graphs-having-problem-in-dots-and-lines%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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





share|improve this answer
























  • That worked very well. Thanks.

    – ASD
    Jan 20 at 16:34











  • 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 at 11:41


















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





share|improve this answer
























  • That worked very well. Thanks.

    – ASD
    Jan 20 at 16:34











  • 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 at 11:41
















1












1








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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 20 at 15:41









Rui BarradasRui Barradas

16.9k51730




16.9k51730













  • That worked very well. Thanks.

    – ASD
    Jan 20 at 16:34











  • 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 at 11:41





















  • That worked very well. Thanks.

    – ASD
    Jan 20 at 16:34











  • 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 at 11:41



















That worked very well. Thanks.

– ASD
Jan 20 at 16:34





That worked very well. Thanks.

– ASD
Jan 20 at 16:34













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 at 11:41







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 at 11:41






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277852%2fadding-two-regression-graphs-having-problem-in-dots-and-lines%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre