Adding two regression graphs having problem in dots and lines
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
|
show 1 more comment
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
Can you post sample data? Please edit the question with the output ofdput(mydata1)
. Or, if it is too big with the output ofdput(head(mydata1, 20))
. And the same withmydata2
.
– 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 withplot(c(0,2), c(0,2), etc)
. The regression lines were outside the plotting area. I solved this problem withwith(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
|
show 1 more comment
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
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
r
asked Jan 20 at 15:15
ASDASD
23
23
Can you post sample data? Please edit the question with the output ofdput(mydata1)
. Or, if it is too big with the output ofdput(head(mydata1, 20))
. And the same withmydata2
.
– 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 withplot(c(0,2), c(0,2), etc)
. The regression lines were outside the plotting area. I solved this problem withwith(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
|
show 1 more comment
Can you post sample data? Please edit the question with the output ofdput(mydata1)
. Or, if it is too big with the output ofdput(head(mydata1, 20))
. And the same withmydata2
.
– 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 withplot(c(0,2), c(0,2), etc)
. The regression lines were outside the plotting area. I solved this problem withwith(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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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")
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))
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 @RuiBarradasrangeX <- 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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")
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))
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 @RuiBarradasrangeX <- 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
add a comment |
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")
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))
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 @RuiBarradasrangeX <- 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
add a comment |
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")
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))
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")
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))
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 @RuiBarradasrangeX <- 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
add a comment |
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 @RuiBarradasrangeX <- 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Can you post sample data? Please edit the question with the output of
dput(mydata1)
. Or, if it is too big with the output ofdput(head(mydata1, 20))
. And the same withmydata2
.– 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 withwith(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