Simpler way to order facet_wrap plot by year









up vote
1
down vote

favorite












I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.



I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.



Is there an easier way ? Here's an example to illustrate what I'm talking about:



sub.data$year = factor(sub.data$year, 
levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))

ggplot(data = sub.data, aes(x = sub.data$weight)) +
geom_histogram() + facet_wrap(~ year, ncol = 2)


Thank you










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.



    I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.



    Is there an easier way ? Here's an example to illustrate what I'm talking about:



    sub.data$year = factor(sub.data$year, 
    levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))

    ggplot(data = sub.data, aes(x = sub.data$weight)) +
    geom_histogram() + facet_wrap(~ year, ncol = 2)


    Thank you










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.



      I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.



      Is there an easier way ? Here's an example to illustrate what I'm talking about:



      sub.data$year = factor(sub.data$year, 
      levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))

      ggplot(data = sub.data, aes(x = sub.data$weight)) +
      geom_histogram() + facet_wrap(~ year, ncol = 2)


      Thank you










      share|improve this question















      I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.



      I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.



      Is there an easier way ? Here's an example to illustrate what I'm talking about:



      sub.data$year = factor(sub.data$year, 
      levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))

      ggplot(data = sub.data, aes(x = sub.data$weight)) +
      geom_histogram() + facet_wrap(~ year, ncol = 2)


      Thank you







      r ggplot2 facet-wrap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 17:40









      Julius Vainora

      29.2k75878




      29.2k75878










      asked Nov 11 at 16:57









      Blue

      213




      213






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          I think you need dir = "v".





          library(tidyverse)

          df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))

          ggplot(df, aes(x, y)) +
          geom_point() +
          facet_wrap(~year, ncol = 2, dir = "v")




          Created on 2018-11-11 by the reprex package (v0.2.1)






          share|improve this answer




















          • Ah! How could I miss this ?
            – Blue
            Nov 12 at 18:43










          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%2f53251046%2fsimpler-way-to-order-facet-wrap-plot-by-year%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
          2
          down vote













          I think you need dir = "v".





          library(tidyverse)

          df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))

          ggplot(df, aes(x, y)) +
          geom_point() +
          facet_wrap(~year, ncol = 2, dir = "v")




          Created on 2018-11-11 by the reprex package (v0.2.1)






          share|improve this answer




















          • Ah! How could I miss this ?
            – Blue
            Nov 12 at 18:43














          up vote
          2
          down vote













          I think you need dir = "v".





          library(tidyverse)

          df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))

          ggplot(df, aes(x, y)) +
          geom_point() +
          facet_wrap(~year, ncol = 2, dir = "v")




          Created on 2018-11-11 by the reprex package (v0.2.1)






          share|improve this answer




















          • Ah! How could I miss this ?
            – Blue
            Nov 12 at 18:43












          up vote
          2
          down vote










          up vote
          2
          down vote









          I think you need dir = "v".





          library(tidyverse)

          df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))

          ggplot(df, aes(x, y)) +
          geom_point() +
          facet_wrap(~year, ncol = 2, dir = "v")




          Created on 2018-11-11 by the reprex package (v0.2.1)






          share|improve this answer












          I think you need dir = "v".





          library(tidyverse)

          df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))

          ggplot(df, aes(x, y)) +
          geom_point() +
          facet_wrap(~year, ncol = 2, dir = "v")




          Created on 2018-11-11 by the reprex package (v0.2.1)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 17:16









          Clemens Hug

          1765




          1765











          • Ah! How could I miss this ?
            – Blue
            Nov 12 at 18:43
















          • Ah! How could I miss this ?
            – Blue
            Nov 12 at 18:43















          Ah! How could I miss this ?
          – Blue
          Nov 12 at 18:43




          Ah! How could I miss this ?
          – Blue
          Nov 12 at 18:43

















          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%2f53251046%2fsimpler-way-to-order-facet-wrap-plot-by-year%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号線