R geom_ribbon not shading area under density curve properly over interval









up vote
1
down vote

favorite












I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.



fig 1



library(ggplot2)
library(plyr)
library(dplyr)
library(tidyr)

x <- seq(0, 43, 0.1)

x_min <- 16
x_max <- 22
df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
sdlog = 0.24))
df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
"Participating", "Not Participating"))
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
gg <- gg + geom_ribbon()
gg <- gg + theme(legend.title = element_blank())
gg









share|improve this question

























    up vote
    1
    down vote

    favorite












    I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.



    fig 1



    library(ggplot2)
    library(plyr)
    library(dplyr)
    library(tidyr)

    x <- seq(0, 43, 0.1)

    x_min <- 16
    x_max <- 22
    df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
    sdlog = 0.24))
    df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
    "Participating", "Not Participating"))
    gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
    gg <- gg + geom_ribbon()
    gg <- gg + theme(legend.title = element_blank())
    gg









    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.



      fig 1



      library(ggplot2)
      library(plyr)
      library(dplyr)
      library(tidyr)

      x <- seq(0, 43, 0.1)

      x_min <- 16
      x_max <- 22
      df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
      sdlog = 0.24))
      df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
      "Participating", "Not Participating"))
      gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
      gg <- gg + geom_ribbon()
      gg <- gg + theme(legend.title = element_blank())
      gg









      share|improve this question













      I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.



      fig 1



      library(ggplot2)
      library(plyr)
      library(dplyr)
      library(tidyr)

      x <- seq(0, 43, 0.1)

      x_min <- 16
      x_max <- 22
      df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
      sdlog = 0.24))
      df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
      "Participating", "Not Participating"))
      gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
      gg <- gg + geom_ribbon()
      gg <- gg + theme(legend.title = element_blank())
      gg






      r ggplot2 density-plot






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 18:13









      ben

      3473923




      3473923






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax where the left area ends and ymax where the right one starts, and the same for ymin. Probably in the past you always used this method for the distribution tails and this problem never arose.



          As to solve this, you are going to need to somehow manually have two geom_ribbon. A not particularly intrusive way would be



          gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
          gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
          gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
          gg <- gg + theme(legend.title = element_blank())


          enter image description here



          Depending on that you wish to do with colors, it potentially can be further modified/simplified.






          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%2f53241976%2fr-geom-ribbon-not-shading-area-under-density-curve-properly-over-interval%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










            The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax where the left area ends and ymax where the right one starts, and the same for ymin. Probably in the past you always used this method for the distribution tails and this problem never arose.



            As to solve this, you are going to need to somehow manually have two geom_ribbon. A not particularly intrusive way would be



            gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
            gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
            gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
            gg <- gg + theme(legend.title = element_blank())


            enter image description here



            Depending on that you wish to do with colors, it potentially can be further modified/simplified.






            share|improve this answer


























              up vote
              1
              down vote



              accepted










              The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax where the left area ends and ymax where the right one starts, and the same for ymin. Probably in the past you always used this method for the distribution tails and this problem never arose.



              As to solve this, you are going to need to somehow manually have two geom_ribbon. A not particularly intrusive way would be



              gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
              gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
              gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
              gg <- gg + theme(legend.title = element_blank())


              enter image description here



              Depending on that you wish to do with colors, it potentially can be further modified/simplified.






              share|improve this answer
























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax where the left area ends and ymax where the right one starts, and the same for ymin. Probably in the past you always used this method for the distribution tails and this problem never arose.



                As to solve this, you are going to need to somehow manually have two geom_ribbon. A not particularly intrusive way would be



                gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
                gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
                gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
                gg <- gg + theme(legend.title = element_blank())


                enter image description here



                Depending on that you wish to do with colors, it potentially can be further modified/simplified.






                share|improve this answer














                The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax where the left area ends and ymax where the right one starts, and the same for ymin. Probably in the past you always used this method for the distribution tails and this problem never arose.



                As to solve this, you are going to need to somehow manually have two geom_ribbon. A not particularly intrusive way would be



                gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
                gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
                gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
                gg <- gg + theme(legend.title = element_blank())


                enter image description here



                Depending on that you wish to do with colors, it potentially can be further modified/simplified.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 17:34

























                answered Nov 10 at 18:37









                Julius Vainora

                26.7k75877




                26.7k75877



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241976%2fr-geom-ribbon-not-shading-area-under-density-curve-properly-over-interval%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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線