ggplot box plot different widths





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a continuous variable that goes between 0 and 1 that I have binned into unequal width bins (all the bins are equal sized except the last which combines values over a threshold). I would like to make a box plot where the width of the box covers the x-range of the bin. Here is a piece of code that makes two plots, one with equal width bins, and one with my binning.



require(ggplot2)
x<-runif(100,0,1)
y<-ifelse(x<0.3,2*x,0.75)+runif(100,0,.15)

xbin <- cut(x = x, breaks = seq(0,1,0.1),include.lowest = T,labels=seq(0.05,0.95,0.1) )
df<-data.frame(x=x,y=y,xbin=xbin)
ggplot(df,aes(x=xbin,y=y))+geom_boxplot()

xbin <- cut(x = x, breaks = c(seq(0,0.3,0.1),1),include.lowest = T,labels=c(seq(0.05,0.25,0.1),">3") )
df<-data.frame(x=x,y=y,xbin=xbin)
ggplot(df,aes(x=xbin,y=y))+geom_boxplot()


I would like the last box to take up the space of all the bins that were merged. I am afraid the that plot is misleading in that the last box covers a much larger x-range. The answer may be that there is a better way of presenting the data. My real data is slightly concentrated at 0 and 1 with fewer points around the 0.5, so I would like to bin the data (unlike the case in How to create geom_boxplot with large amount of continuous x-variables).



Thank you










share|improve this question





























    1















    I have a continuous variable that goes between 0 and 1 that I have binned into unequal width bins (all the bins are equal sized except the last which combines values over a threshold). I would like to make a box plot where the width of the box covers the x-range of the bin. Here is a piece of code that makes two plots, one with equal width bins, and one with my binning.



    require(ggplot2)
    x<-runif(100,0,1)
    y<-ifelse(x<0.3,2*x,0.75)+runif(100,0,.15)

    xbin <- cut(x = x, breaks = seq(0,1,0.1),include.lowest = T,labels=seq(0.05,0.95,0.1) )
    df<-data.frame(x=x,y=y,xbin=xbin)
    ggplot(df,aes(x=xbin,y=y))+geom_boxplot()

    xbin <- cut(x = x, breaks = c(seq(0,0.3,0.1),1),include.lowest = T,labels=c(seq(0.05,0.25,0.1),">3") )
    df<-data.frame(x=x,y=y,xbin=xbin)
    ggplot(df,aes(x=xbin,y=y))+geom_boxplot()


    I would like the last box to take up the space of all the bins that were merged. I am afraid the that plot is misleading in that the last box covers a much larger x-range. The answer may be that there is a better way of presenting the data. My real data is slightly concentrated at 0 and 1 with fewer points around the 0.5, so I would like to bin the data (unlike the case in How to create geom_boxplot with large amount of continuous x-variables).



    Thank you










    share|improve this question

























      1












      1








      1








      I have a continuous variable that goes between 0 and 1 that I have binned into unequal width bins (all the bins are equal sized except the last which combines values over a threshold). I would like to make a box plot where the width of the box covers the x-range of the bin. Here is a piece of code that makes two plots, one with equal width bins, and one with my binning.



      require(ggplot2)
      x<-runif(100,0,1)
      y<-ifelse(x<0.3,2*x,0.75)+runif(100,0,.15)

      xbin <- cut(x = x, breaks = seq(0,1,0.1),include.lowest = T,labels=seq(0.05,0.95,0.1) )
      df<-data.frame(x=x,y=y,xbin=xbin)
      ggplot(df,aes(x=xbin,y=y))+geom_boxplot()

      xbin <- cut(x = x, breaks = c(seq(0,0.3,0.1),1),include.lowest = T,labels=c(seq(0.05,0.25,0.1),">3") )
      df<-data.frame(x=x,y=y,xbin=xbin)
      ggplot(df,aes(x=xbin,y=y))+geom_boxplot()


      I would like the last box to take up the space of all the bins that were merged. I am afraid the that plot is misleading in that the last box covers a much larger x-range. The answer may be that there is a better way of presenting the data. My real data is slightly concentrated at 0 and 1 with fewer points around the 0.5, so I would like to bin the data (unlike the case in How to create geom_boxplot with large amount of continuous x-variables).



      Thank you










      share|improve this question














      I have a continuous variable that goes between 0 and 1 that I have binned into unequal width bins (all the bins are equal sized except the last which combines values over a threshold). I would like to make a box plot where the width of the box covers the x-range of the bin. Here is a piece of code that makes two plots, one with equal width bins, and one with my binning.



      require(ggplot2)
      x<-runif(100,0,1)
      y<-ifelse(x<0.3,2*x,0.75)+runif(100,0,.15)

      xbin <- cut(x = x, breaks = seq(0,1,0.1),include.lowest = T,labels=seq(0.05,0.95,0.1) )
      df<-data.frame(x=x,y=y,xbin=xbin)
      ggplot(df,aes(x=xbin,y=y))+geom_boxplot()

      xbin <- cut(x = x, breaks = c(seq(0,0.3,0.1),1),include.lowest = T,labels=c(seq(0.05,0.25,0.1),">3") )
      df<-data.frame(x=x,y=y,xbin=xbin)
      ggplot(df,aes(x=xbin,y=y))+geom_boxplot()


      I would like the last box to take up the space of all the bins that were merged. I am afraid the that plot is misleading in that the last box covers a much larger x-range. The answer may be that there is a better way of presenting the data. My real data is slightly concentrated at 0 and 1 with fewer points around the 0.5, so I would like to bin the data (unlike the case in How to create geom_boxplot with large amount of continuous x-variables).



      Thank you







      r ggplot2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 16:14









      HowYaDoingHowYaDoing

      495312




      495312
























          1 Answer
          1






          active

          oldest

          votes


















          1














          like this?



          ggplot( data = df, aes( x = x, y = y, colour = xbin ) ) + geom_boxplot()


          enter image description here



          of perhaps a violin-plot?



          ggplot( data = df, aes( x = x, y = y, colour = xbin)) + geom_violin() + geom_point( alpha = 0.5 )


          enter image description here






          share|improve this answer


























          • exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

            – HowYaDoing
            Jan 4 at 17:12








          • 1





            @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

            – Wimpel
            Jan 4 at 17:26






          • 1





            by the way, group=xbin works also (with no warning). Thank you @Wimpel

            – HowYaDoing
            Jan 4 at 17:34












          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%2f54042551%2fggplot-box-plot-different-widths%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














          like this?



          ggplot( data = df, aes( x = x, y = y, colour = xbin ) ) + geom_boxplot()


          enter image description here



          of perhaps a violin-plot?



          ggplot( data = df, aes( x = x, y = y, colour = xbin)) + geom_violin() + geom_point( alpha = 0.5 )


          enter image description here






          share|improve this answer


























          • exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

            – HowYaDoing
            Jan 4 at 17:12








          • 1





            @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

            – Wimpel
            Jan 4 at 17:26






          • 1





            by the way, group=xbin works also (with no warning). Thank you @Wimpel

            – HowYaDoing
            Jan 4 at 17:34
















          1














          like this?



          ggplot( data = df, aes( x = x, y = y, colour = xbin ) ) + geom_boxplot()


          enter image description here



          of perhaps a violin-plot?



          ggplot( data = df, aes( x = x, y = y, colour = xbin)) + geom_violin() + geom_point( alpha = 0.5 )


          enter image description here






          share|improve this answer


























          • exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

            – HowYaDoing
            Jan 4 at 17:12








          • 1





            @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

            – Wimpel
            Jan 4 at 17:26






          • 1





            by the way, group=xbin works also (with no warning). Thank you @Wimpel

            – HowYaDoing
            Jan 4 at 17:34














          1












          1








          1







          like this?



          ggplot( data = df, aes( x = x, y = y, colour = xbin ) ) + geom_boxplot()


          enter image description here



          of perhaps a violin-plot?



          ggplot( data = df, aes( x = x, y = y, colour = xbin)) + geom_violin() + geom_point( alpha = 0.5 )


          enter image description here






          share|improve this answer















          like this?



          ggplot( data = df, aes( x = x, y = y, colour = xbin ) ) + geom_boxplot()


          enter image description here



          of perhaps a violin-plot?



          ggplot( data = df, aes( x = x, y = y, colour = xbin)) + geom_violin() + geom_point( alpha = 0.5 )


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 17:04

























          answered Jan 4 at 16:59









          WimpelWimpel

          6,362323




          6,362323













          • exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

            – HowYaDoing
            Jan 4 at 17:12








          • 1





            @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

            – Wimpel
            Jan 4 at 17:26






          • 1





            by the way, group=xbin works also (with no warning). Thank you @Wimpel

            – HowYaDoing
            Jan 4 at 17:34



















          • exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

            – HowYaDoing
            Jan 4 at 17:12








          • 1





            @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

            – Wimpel
            Jan 4 at 17:26






          • 1





            by the way, group=xbin works also (with no warning). Thank you @Wimpel

            – HowYaDoing
            Jan 4 at 17:34

















          exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

          – HowYaDoing
          Jan 4 at 17:12







          exactly, but I want the color to be the same. This is for a publication. Any problem with using aes(...,alpha=xbin). It gives a warning but looks ... mostly... good.

          – HowYaDoing
          Jan 4 at 17:12






          1




          1





          @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

          – Wimpel
          Jan 4 at 17:26





          @HowYaDoing add + scale_colour_manual(values= rep("black", 4) )?

          – Wimpel
          Jan 4 at 17:26




          1




          1





          by the way, group=xbin works also (with no warning). Thank you @Wimpel

          – HowYaDoing
          Jan 4 at 17:34





          by the way, group=xbin works also (with no warning). Thank you @Wimpel

          – HowYaDoing
          Jan 4 at 17:34




















          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%2f54042551%2fggplot-box-plot-different-widths%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

          Mossoró

          Cannot access a disposed object : DataContext

          Can't read property showImagePicker of undefined in react native iOS