Plot y = mx + c with ggplot
up vote
5
down vote
favorite
Basic question, ggplot doesn't seem to be doing what I expect though.
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
I'm expecting this to plot :
It's plotting :
r ggplot2
add a comment |
up vote
5
down vote
favorite
Basic question, ggplot doesn't seem to be doing what I expect though.
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
I'm expecting this to plot :
It's plotting :
r ggplot2
3
Then you may need to clarify "the correct result". Intercept in a linear regression is commonly defined as the value of y when x is zero. Your abline clearly runs through (x = 0, y = 1). The limits of the plot area is given by the range of your data.
– Henrik
Nov 11 at 14:23
Also please read the help text?geom_abline
: "If you use arguments, e.g.geom_abline(intercept = 0, slope = 1)
, then behind the scenes the geom makes a new data frame containing just the data you've supplied"
– Henrik
Nov 11 at 14:37
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Basic question, ggplot doesn't seem to be doing what I expect though.
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
I'm expecting this to plot :
It's plotting :
r ggplot2
Basic question, ggplot doesn't seem to be doing what I expect though.
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
I'm expecting this to plot :
It's plotting :
r ggplot2
r ggplot2
edited Nov 11 at 14:28
asked Nov 11 at 13:48
baxx
368420
368420
3
Then you may need to clarify "the correct result". Intercept in a linear regression is commonly defined as the value of y when x is zero. Your abline clearly runs through (x = 0, y = 1). The limits of the plot area is given by the range of your data.
– Henrik
Nov 11 at 14:23
Also please read the help text?geom_abline
: "If you use arguments, e.g.geom_abline(intercept = 0, slope = 1)
, then behind the scenes the geom makes a new data frame containing just the data you've supplied"
– Henrik
Nov 11 at 14:37
add a comment |
3
Then you may need to clarify "the correct result". Intercept in a linear regression is commonly defined as the value of y when x is zero. Your abline clearly runs through (x = 0, y = 1). The limits of the plot area is given by the range of your data.
– Henrik
Nov 11 at 14:23
Also please read the help text?geom_abline
: "If you use arguments, e.g.geom_abline(intercept = 0, slope = 1)
, then behind the scenes the geom makes a new data frame containing just the data you've supplied"
– Henrik
Nov 11 at 14:37
3
3
Then you may need to clarify "the correct result". Intercept in a linear regression is commonly defined as the value of y when x is zero. Your abline clearly runs through (x = 0, y = 1). The limits of the plot area is given by the range of your data.
– Henrik
Nov 11 at 14:23
Then you may need to clarify "the correct result". Intercept in a linear regression is commonly defined as the value of y when x is zero. Your abline clearly runs through (x = 0, y = 1). The limits of the plot area is given by the range of your data.
– Henrik
Nov 11 at 14:23
Also please read the help text
?geom_abline
: "If you use arguments, e.g. geom_abline(intercept = 0, slope = 1)
, then behind the scenes the geom makes a new data frame containing just the data you've supplied"– Henrik
Nov 11 at 14:37
Also please read the help text
?geom_abline
: "If you use arguments, e.g. geom_abline(intercept = 0, slope = 1)
, then behind the scenes the geom makes a new data frame containing just the data you've supplied"– Henrik
Nov 11 at 14:37
add a comment |
2 Answers
2
active
oldest
votes
up vote
10
down vote
accepted
The plot ggplot2
draws is not incorrect. It draws the function over the scales necessary to represent the data you pass to the aes
call. It doesn't care whether you actually draw the data in a geom
or not.
To illustrate the issue, it is helpful to add the actual data points to a plot and make the x- and y-axis more visible. The code below
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_point(shape = 1) +
geom_abline(intercept = 1, slope = -1, col = "red") +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0)
gives you:
Since you only want to plot a subsection of the above plot, just correct the scales (and don't draw the axes and datapoints). Then you get the result you desire:
ggplot(data=data.frame(x=c(-1,2), y=c(-1,2)), aes(x=x,y=y)) +
geom_blank() + # not necessary, taken from the OP's question
geom_abline(intercept = 1, slope = -1) +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
1
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
I learned something about setting the limits inggplot2
but wouldn't it have been easier to justggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going throughgeom_blank
in the first place. Is there some subtility i am missing?
– Jrakru56
Nov 11 at 14:57
add a comment |
up vote
3
down vote
I think ggplot2 does exactly what you ask it to do: you draw an empty canvans with goes from (-1, -1) to (2, 2) and then you add a abline. If you want to match the canvans to your exampe, just adjust the coordinates of the points that you specify:
library(tidyverse)
ggplot(data=data.frame( x=c(0,2),y=c(1,0)), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
The plot ggplot2
draws is not incorrect. It draws the function over the scales necessary to represent the data you pass to the aes
call. It doesn't care whether you actually draw the data in a geom
or not.
To illustrate the issue, it is helpful to add the actual data points to a plot and make the x- and y-axis more visible. The code below
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_point(shape = 1) +
geom_abline(intercept = 1, slope = -1, col = "red") +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0)
gives you:
Since you only want to plot a subsection of the above plot, just correct the scales (and don't draw the axes and datapoints). Then you get the result you desire:
ggplot(data=data.frame(x=c(-1,2), y=c(-1,2)), aes(x=x,y=y)) +
geom_blank() + # not necessary, taken from the OP's question
geom_abline(intercept = 1, slope = -1) +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
1
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
I learned something about setting the limits inggplot2
but wouldn't it have been easier to justggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going throughgeom_blank
in the first place. Is there some subtility i am missing?
– Jrakru56
Nov 11 at 14:57
add a comment |
up vote
10
down vote
accepted
The plot ggplot2
draws is not incorrect. It draws the function over the scales necessary to represent the data you pass to the aes
call. It doesn't care whether you actually draw the data in a geom
or not.
To illustrate the issue, it is helpful to add the actual data points to a plot and make the x- and y-axis more visible. The code below
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_point(shape = 1) +
geom_abline(intercept = 1, slope = -1, col = "red") +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0)
gives you:
Since you only want to plot a subsection of the above plot, just correct the scales (and don't draw the axes and datapoints). Then you get the result you desire:
ggplot(data=data.frame(x=c(-1,2), y=c(-1,2)), aes(x=x,y=y)) +
geom_blank() + # not necessary, taken from the OP's question
geom_abline(intercept = 1, slope = -1) +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
1
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
I learned something about setting the limits inggplot2
but wouldn't it have been easier to justggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going throughgeom_blank
in the first place. Is there some subtility i am missing?
– Jrakru56
Nov 11 at 14:57
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
The plot ggplot2
draws is not incorrect. It draws the function over the scales necessary to represent the data you pass to the aes
call. It doesn't care whether you actually draw the data in a geom
or not.
To illustrate the issue, it is helpful to add the actual data points to a plot and make the x- and y-axis more visible. The code below
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_point(shape = 1) +
geom_abline(intercept = 1, slope = -1, col = "red") +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0)
gives you:
Since you only want to plot a subsection of the above plot, just correct the scales (and don't draw the axes and datapoints). Then you get the result you desire:
ggplot(data=data.frame(x=c(-1,2), y=c(-1,2)), aes(x=x,y=y)) +
geom_blank() + # not necessary, taken from the OP's question
geom_abline(intercept = 1, slope = -1) +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
The plot ggplot2
draws is not incorrect. It draws the function over the scales necessary to represent the data you pass to the aes
call. It doesn't care whether you actually draw the data in a geom
or not.
To illustrate the issue, it is helpful to add the actual data points to a plot and make the x- and y-axis more visible. The code below
ggplot(data=data.frame( x=c(-1,2),y=c(-1,2) ), aes(x=x,y=y)) +
geom_point(shape = 1) +
geom_abline(intercept = 1, slope = -1, col = "red") +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0)
gives you:
Since you only want to plot a subsection of the above plot, just correct the scales (and don't draw the axes and datapoints). Then you get the result you desire:
ggplot(data=data.frame(x=c(-1,2), y=c(-1,2)), aes(x=x,y=y)) +
geom_blank() + # not necessary, taken from the OP's question
geom_abline(intercept = 1, slope = -1) +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
edited Nov 11 at 15:01
answered Nov 11 at 14:34
apitsch
567216
567216
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
1
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
I learned something about setting the limits inggplot2
but wouldn't it have been easier to justggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going throughgeom_blank
in the first place. Is there some subtility i am missing?
– Jrakru56
Nov 11 at 14:57
add a comment |
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
1
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
I learned something about setting the limits inggplot2
but wouldn't it have been easier to justggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going throughgeom_blank
in the first place. Is there some subtility i am missing?
– Jrakru56
Nov 11 at 14:57
1
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 at 14:40
1
1
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
the problem was basic interpretation, as markus seems to have made a similar error. I feel that having something basic like this that directly answers how to plot y=mx + c might be useful to others in future though. In this case, plotting the axis with geom_hline() and geom_vline() would have been enough to demonstrate that the graph was in fact correct, and that I am in fact too tired.
– baxx
Nov 11 at 14:42
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
@apitsch perhaps you could consider adding the case of there being axis lines to more clearly demonstrate the problem. Here's a link to a plot you could use : i.imgur.com/kh4FLaw.png , here's a link to the code : vpaste.net/IVLln
– baxx
Nov 11 at 14:48
I learned something about setting the limits in
ggplot2
but wouldn't it have been easier to just ggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going through geom_blank
in the first place. Is there some subtility i am missing?– Jrakru56
Nov 11 at 14:57
I learned something about setting the limits in
ggplot2
but wouldn't it have been easier to just ggplot()+ geom_abline(intercept = 1, slope = -1) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1))
instead of going through geom_blank
in the first place. Is there some subtility i am missing?– Jrakru56
Nov 11 at 14:57
add a comment |
up vote
3
down vote
I think ggplot2 does exactly what you ask it to do: you draw an empty canvans with goes from (-1, -1) to (2, 2) and then you add a abline. If you want to match the canvans to your exampe, just adjust the coordinates of the points that you specify:
library(tidyverse)
ggplot(data=data.frame( x=c(0,2),y=c(1,0)), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
add a comment |
up vote
3
down vote
I think ggplot2 does exactly what you ask it to do: you draw an empty canvans with goes from (-1, -1) to (2, 2) and then you add a abline. If you want to match the canvans to your exampe, just adjust the coordinates of the points that you specify:
library(tidyverse)
ggplot(data=data.frame( x=c(0,2),y=c(1,0)), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
add a comment |
up vote
3
down vote
up vote
3
down vote
I think ggplot2 does exactly what you ask it to do: you draw an empty canvans with goes from (-1, -1) to (2, 2) and then you add a abline. If you want to match the canvans to your exampe, just adjust the coordinates of the points that you specify:
library(tidyverse)
ggplot(data=data.frame( x=c(0,2),y=c(1,0)), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
I think ggplot2 does exactly what you ask it to do: you draw an empty canvans with goes from (-1, -1) to (2, 2) and then you add a abline. If you want to match the canvans to your exampe, just adjust the coordinates of the points that you specify:
library(tidyverse)
ggplot(data=data.frame( x=c(0,2),y=c(1,0)), aes(x=x,y=y)) +
geom_blank() +
geom_abline(slope = -1 , intercept = 1)
answered Nov 11 at 14:33
JReddig
7117
7117
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53249392%2fplot-y-mx-c-with-ggplot%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
3
Then you may need to clarify "the correct result". Intercept in a linear regression is commonly defined as the value of y when x is zero. Your abline clearly runs through (x = 0, y = 1). The limits of the plot area is given by the range of your data.
– Henrik
Nov 11 at 14:23
Also please read the help text
?geom_abline
: "If you use arguments, e.g.geom_abline(intercept = 0, slope = 1)
, then behind the scenes the geom makes a new data frame containing just the data you've supplied"– Henrik
Nov 11 at 14:37