geom_histogram and geom_blank: controlling y limits












0















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 '18 at 21:44











  • Yes. It works. Awesome. Thanks.

    – user13424
    Nov 23 '18 at 13:44
















0















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 '18 at 21:44











  • Yes. It works. Awesome. Thanks.

    – user13424
    Nov 23 '18 at 13:44














0












0








0








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 '18 at 21:16









markus

14.8k11336




14.8k11336










asked Nov 21 '18 at 20:58









user13424user13424

324




324













  • Did my answer help you solve your issue?

    – markus
    Nov 22 '18 at 21:44











  • Yes. It works. Awesome. Thanks.

    – user13424
    Nov 23 '18 at 13:44



















  • Did my answer help you solve your issue?

    – markus
    Nov 22 '18 at 21:44











  • Yes. It works. Awesome. Thanks.

    – user13424
    Nov 23 '18 at 13:44

















Did my answer help you solve your issue?

– markus
Nov 22 '18 at 21:44





Did my answer help you solve your issue?

– markus
Nov 22 '18 at 21:44













Yes. It works. Awesome. Thanks.

– user13424
Nov 23 '18 at 13:44





Yes. It works. Awesome. Thanks.

– user13424
Nov 23 '18 at 13:44












1 Answer
1






active

oldest

votes


















1














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',
    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
    });


    }
    });














    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









    1














    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






























      1














      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




























        1












        1








        1







        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 '18 at 21:21

























        answered Nov 21 '18 at 21:15









        markusmarkus

        14.8k11336




        14.8k11336
































            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.




            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

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?