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 :



enter image description here



It's plotting :



enter image description here










share|improve this question



















  • 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














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 :



enter image description here



It's plotting :



enter image description here










share|improve this question



















  • 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












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 :



enter image description here



It's plotting :



enter image description here










share|improve this question















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 :



enter image description here



It's plotting :



enter image description here







r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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:
enter image description here



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


enter image description here






share|improve this answer


















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

















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)





share|improve this answer




















    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',
    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%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








    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:
    enter image description here



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


    enter image description here






    share|improve this answer


















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














    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:
    enter image description here



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


    enter image description here






    share|improve this answer


















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












    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:
    enter image description here



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


    enter image description here






    share|improve this answer














    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:
    enter image description here



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


    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








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












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







    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












    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)





    share|improve this answer
























      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)





      share|improve this answer






















        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)





        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 14:33









        JReddig

        7117




        7117



























            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.





            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto