geom_histogram and geom_blank: controlling y limits











up vote
0
down vote

favorite












I want to create a set of histograms that will make use of facet_wrap. The y limits vary a lot, sometimes there is no data for a given combination. So, I thought I could control the y limits using a dummy data frame and geom_blank.



Geom_blank() works as expected when I use geom_point() but not when when I use geom_histogram()



For example, I create these two data frames



test  <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2), 
y.value=rep(1:3, each=2))
dummy <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2),
y.value=c(0,10,10,20,20,30))


If I want to do a scatterplot:



p1 <- ggplot(test, aes(x=x.value, y=y.value)) + geom_point()
p1 <- p1 + facet_wrap(~grp, scales="free")
p1

p2 <- p1 + geom_blank(data=dummy)
p2


geom_blank() changed the y limits.



But when I try to create a histogram



p3 <- ggplot(test, aes(x=x.value, weight=y.value)) + geom_histogram(bins=6)
p3 <- p3 + facet_wrap(~grp, scales="free")
p3

p4 <- p3 + geom_blank(data=dummy)
p4


geom_blank() has no impact.



How can I control complex y limits on a set of histograms?










share|improve this question
























  • Did my answer help you solve your issue?
    – markus
    Nov 22 at 21:44










  • Yes. It works. Awesome. Thanks.
    – user13424
    Nov 23 at 13:44















up vote
0
down vote

favorite












I want to create a set of histograms that will make use of facet_wrap. The y limits vary a lot, sometimes there is no data for a given combination. So, I thought I could control the y limits using a dummy data frame and geom_blank.



Geom_blank() works as expected when I use geom_point() but not when when I use geom_histogram()



For example, I create these two data frames



test  <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2), 
y.value=rep(1:3, each=2))
dummy <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2),
y.value=c(0,10,10,20,20,30))


If I want to do a scatterplot:



p1 <- ggplot(test, aes(x=x.value, y=y.value)) + geom_point()
p1 <- p1 + facet_wrap(~grp, scales="free")
p1

p2 <- p1 + geom_blank(data=dummy)
p2


geom_blank() changed the y limits.



But when I try to create a histogram



p3 <- ggplot(test, aes(x=x.value, weight=y.value)) + geom_histogram(bins=6)
p3 <- p3 + facet_wrap(~grp, scales="free")
p3

p4 <- p3 + geom_blank(data=dummy)
p4


geom_blank() has no impact.



How can I control complex y limits on a set of histograms?










share|improve this question
























  • Did my answer help you solve your issue?
    – markus
    Nov 22 at 21:44










  • Yes. It works. Awesome. Thanks.
    – user13424
    Nov 23 at 13:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to create a set of histograms that will make use of facet_wrap. The y limits vary a lot, sometimes there is no data for a given combination. So, I thought I could control the y limits using a dummy data frame and geom_blank.



Geom_blank() works as expected when I use geom_point() but not when when I use geom_histogram()



For example, I create these two data frames



test  <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2), 
y.value=rep(1:3, each=2))
dummy <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2),
y.value=c(0,10,10,20,20,30))


If I want to do a scatterplot:



p1 <- ggplot(test, aes(x=x.value, y=y.value)) + geom_point()
p1 <- p1 + facet_wrap(~grp, scales="free")
p1

p2 <- p1 + geom_blank(data=dummy)
p2


geom_blank() changed the y limits.



But when I try to create a histogram



p3 <- ggplot(test, aes(x=x.value, weight=y.value)) + geom_histogram(bins=6)
p3 <- p3 + facet_wrap(~grp, scales="free")
p3

p4 <- p3 + geom_blank(data=dummy)
p4


geom_blank() has no impact.



How can I control complex y limits on a set of histograms?










share|improve this question















I want to create a set of histograms that will make use of facet_wrap. The y limits vary a lot, sometimes there is no data for a given combination. So, I thought I could control the y limits using a dummy data frame and geom_blank.



Geom_blank() works as expected when I use geom_point() but not when when I use geom_histogram()



For example, I create these two data frames



test  <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2), 
y.value=rep(1:3, each=2))
dummy <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2),
y.value=c(0,10,10,20,20,30))


If I want to do a scatterplot:



p1 <- ggplot(test, aes(x=x.value, y=y.value)) + geom_point()
p1 <- p1 + facet_wrap(~grp, scales="free")
p1

p2 <- p1 + geom_blank(data=dummy)
p2


geom_blank() changed the y limits.



But when I try to create a histogram



p3 <- ggplot(test, aes(x=x.value, weight=y.value)) + geom_histogram(bins=6)
p3 <- p3 + facet_wrap(~grp, scales="free")
p3

p4 <- p3 + geom_blank(data=dummy)
p4


geom_blank() has no impact.



How can I control complex y limits on a set of histograms?







r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 21:16









markus

8,565928




8,565928










asked Nov 21 at 20:58









user13424

324




324












  • Did my answer help you solve your issue?
    – markus
    Nov 22 at 21:44










  • Yes. It works. Awesome. Thanks.
    – user13424
    Nov 23 at 13:44


















  • Did my answer help you solve your issue?
    – markus
    Nov 22 at 21:44










  • Yes. It works. Awesome. Thanks.
    – user13424
    Nov 23 at 13:44
















Did my answer help you solve your issue?
– markus
Nov 22 at 21:44




Did my answer help you solve your issue?
– markus
Nov 22 at 21:44












Yes. It works. Awesome. Thanks.
– user13424
Nov 23 at 13:44




Yes. It works. Awesome. Thanks.
– user13424
Nov 23 at 13:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You need to map y.value to y like you did in p1 and p2.



p4 <- p3 + geom_blank(aes(y = y.value), data=dummy)
p4


enter image description here






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%2f53420379%2fgeom-histogram-and-geom-blank-controlling-y-limits%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








    up vote
    1
    down vote



    accepted










    You need to map y.value to y like you did in p1 and p2.



    p4 <- p3 + geom_blank(aes(y = y.value), data=dummy)
    p4


    enter image description here






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      You need to map y.value to y like you did in p1 and p2.



      p4 <- p3 + geom_blank(aes(y = y.value), data=dummy)
      p4


      enter image description here






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You need to map y.value to y like you did in p1 and p2.



        p4 <- p3 + geom_blank(aes(y = y.value), data=dummy)
        p4


        enter image description here






        share|improve this answer














        You need to map y.value to y like you did in p1 and p2.



        p4 <- p3 + geom_blank(aes(y = y.value), data=dummy)
        p4


        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 21:21

























        answered Nov 21 at 21:15









        markus

        8,565928




        8,565928






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420379%2fgeom-histogram-and-geom-blank-controlling-y-limits%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu