How to plot a boxplot with correctly spaced continuous x-axis values in ggplot2









up vote
1
down vote

favorite












I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:



library(ggplot2)
set.seed(1234)
#get data
df <- data.frame(y=abs(rnorm(8)),
x=as.factor(rep(c(0,100,200,500),times=2)))
ggplot(aes(y=y,x=x), data=df) +
geom_boxplot()


This gives me the plot



enter image description here



where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:



    library(ggplot2)
    set.seed(1234)
    #get data
    df <- data.frame(y=abs(rnorm(8)),
    x=as.factor(rep(c(0,100,200,500),times=2)))
    ggplot(aes(y=y,x=x), data=df) +
    geom_boxplot()


    This gives me the plot



    enter image description here



    where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:



      library(ggplot2)
      set.seed(1234)
      #get data
      df <- data.frame(y=abs(rnorm(8)),
      x=as.factor(rep(c(0,100,200,500),times=2)))
      ggplot(aes(y=y,x=x), data=df) +
      geom_boxplot()


      This gives me the plot



      enter image description here



      where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.










      share|improve this question















      I am attempting to plot a boxplot where my x-axis is a continuous time-scale which is growing degree days i.e. 0 to 2500. I would like to get a boxplot with x-axis values correctly spaced on a continuous time-scale rather than a discrete one. Normally if it were a regular time/date, I could have used a 'scale_x_date' with ggplot2 in R. However, since the numbers are outside date/time scale I am not sure how can we correctly space the x-axis values. Here is the dummy example:



      library(ggplot2)
      set.seed(1234)
      #get data
      df <- data.frame(y=abs(rnorm(8)),
      x=as.factor(rep(c(0,100,200,500),times=2)))
      ggplot(aes(y=y,x=x), data=df) +
      geom_boxplot()


      This gives me the plot



      enter image description here



      where my x-axis is not spaced based on its numeric values. Instead, I would like to get a boxplot where the spacing between 200 to 500 should be three times more than 100-200. My actual data has x-axis values ranging 0-2500 growing days. I am looking for ggplot2 specific solution preferably.







      r ggplot2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 2:20

























      asked Nov 11 at 2:06









      SinghD

      154




      154






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          df <- data.frame(y=abs(rnorm(8)),
          x=rep(c(0,100,200,500),times=2))

          ggplot(df, aes(x, y, group=x)) +
          geom_boxplot()


          enter image description here



          This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.






          share|improve this answer






















          • This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
            – SinghD
            Nov 11 at 2:54










          • you can use the width= argument to geom_boxplot. Set the width in x units
            – dww
            Nov 11 at 3:44










          • I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
            – Tjebo
            Nov 11 at 11:50










          • @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
            – dww
            Nov 11 at 16:00










          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%2f53245233%2fhow-to-plot-a-boxplot-with-correctly-spaced-continuous-x-axis-values-in-ggplot2%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










          df <- data.frame(y=abs(rnorm(8)),
          x=rep(c(0,100,200,500),times=2))

          ggplot(df, aes(x, y, group=x)) +
          geom_boxplot()


          enter image description here



          This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.






          share|improve this answer






















          • This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
            – SinghD
            Nov 11 at 2:54










          • you can use the width= argument to geom_boxplot. Set the width in x units
            – dww
            Nov 11 at 3:44










          • I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
            – Tjebo
            Nov 11 at 11:50










          • @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
            – dww
            Nov 11 at 16:00














          up vote
          1
          down vote



          accepted










          df <- data.frame(y=abs(rnorm(8)),
          x=rep(c(0,100,200,500),times=2))

          ggplot(df, aes(x, y, group=x)) +
          geom_boxplot()


          enter image description here



          This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.






          share|improve this answer






















          • This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
            – SinghD
            Nov 11 at 2:54










          • you can use the width= argument to geom_boxplot. Set the width in x units
            – dww
            Nov 11 at 3:44










          • I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
            – Tjebo
            Nov 11 at 11:50










          • @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
            – dww
            Nov 11 at 16:00












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          df <- data.frame(y=abs(rnorm(8)),
          x=rep(c(0,100,200,500),times=2))

          ggplot(df, aes(x, y, group=x)) +
          geom_boxplot()


          enter image description here



          This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.






          share|improve this answer














          df <- data.frame(y=abs(rnorm(8)),
          x=rep(c(0,100,200,500),times=2))

          ggplot(df, aes(x, y, group=x)) +
          geom_boxplot()


          enter image description here



          This solution relies on two changes. First, to plot boxes positioned on a continuous x axis, we need to provide numeric rather than factor x values. However, this does not work by itself, because without x values being grouped by factor levels, ggplot no longer knows how to group the data into different boxes. So, we also need to provide an additional grouping variable.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 15:58

























          answered Nov 11 at 2:21









          dww

          14.2k22655




          14.2k22655











          • This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
            – SinghD
            Nov 11 at 2:54










          • you can use the width= argument to geom_boxplot. Set the width in x units
            – dww
            Nov 11 at 3:44










          • I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
            – Tjebo
            Nov 11 at 11:50










          • @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
            – dww
            Nov 11 at 16:00
















          • This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
            – SinghD
            Nov 11 at 2:54










          • you can use the width= argument to geom_boxplot. Set the width in x units
            – dww
            Nov 11 at 3:44










          • I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
            – Tjebo
            Nov 11 at 11:50










          • @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
            – dww
            Nov 11 at 16:00















          This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
          – SinghD
          Nov 11 at 2:54




          This works great. On a side note, my actual data has a longer timescale (0 to 2500), which on the full plot stretches the axis to an extent that it makes the boxplots almost look like thin lines. I have tried scaling my axis to '100 units by dividing x by 100. But it merges the closer points on x-axis into one point. Do you have any suggestion to remedy this issue?
          – SinghD
          Nov 11 at 2:54












          you can use the width= argument to geom_boxplot. Set the width in x units
          – dww
          Nov 11 at 3:44




          you can use the width= argument to geom_boxplot. Set the width in x units
          – dww
          Nov 11 at 3:44












          I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
          – Tjebo
          Nov 11 at 11:50




          I think it might be worth to point out that the important step here was to change the x variable in the sample data from categorical to continuous .
          – Tjebo
          Nov 11 at 11:50












          @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
          – dww
          Nov 11 at 16:00




          @Tjebotj - thanks, i added that info to the answer. Also that it requires an additional step beyond that.
          – dww
          Nov 11 at 16:00

















          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%2f53245233%2fhow-to-plot-a-boxplot-with-correctly-spaced-continuous-x-axis-values-in-ggplot2%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo