Plot y = mx + c with ggplot
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 |
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 '18 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 '18 at 14:37
add a comment |
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 '18 at 14:28
baxx
asked Nov 11 '18 at 13:48
baxxbaxx
403522
403522
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 14:37
add a comment |
2 Answers
2
active
oldest
votes
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 '18 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 '18 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 '18 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 '18 at 14:57
add a comment |
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 |
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%2f53249392%2fplot-y-mx-c-with-ggplot%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 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 '18 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 '18 at 14:57
add a comment |
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 '18 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 '18 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 '18 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 '18 at 14:57
add a comment |
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 '18 at 15:01
answered Nov 11 '18 at 14:34
apitschapitsch
582216
582216
1
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 14:40
@baxx Do you happen to remember this post: How to set limits for axes in ggplot2 R plots? ;)
– Henrik
Nov 11 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 14:57
add a comment |
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 |
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 |
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 '18 at 14:33
JReddigJReddig
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.
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 '18 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 '18 at 14:37