facet_wrap equal axis per panel





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







0















I would like to make a plot using facet_wrap where the axes can vary for each panel but within a panel the x and y axes should be the same scale.



e.g. see the following plots



df <- read.table(text = "
x y g
1 5 a
2 6 a
3 7 a
4 8 a
5 9 b
6 10 b
7 11 b
8 12 b", header = TRUE)

library(ggplot2)

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g) # all axes 1-12

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g, scales = "free")
# fee axes, y & y axes don't match per panel


What i want is for panel a the x and why axes both to be 1-8 and for panel b the x and y axes both to range from 5 - 12.



Is this possible?










share|improve this question























  • I wonder if you are keen to draw two separate figures and use grid.arrange(). That would be one way to go, I think.

    – jazzurro
    Feb 27 '15 at 11:04











  • @jazzurro I was thinking about employing grid.arrange although it seems a bit frustrating when you have 8-12 panels.

    – user1320502
    Feb 27 '15 at 11:10













  • I see your point. But, I'd be happy to use ncol and nrow in grid.arrange rather than thinking about how I can manipulate x and y axis for each figure.

    – jazzurro
    Feb 27 '15 at 11:20











  • Yeah i just wrote a apply to do this, I'll just find out how to match the x and y limits for a ggplot then i could use that

    – user1320502
    Feb 27 '15 at 11:28


















0















I would like to make a plot using facet_wrap where the axes can vary for each panel but within a panel the x and y axes should be the same scale.



e.g. see the following plots



df <- read.table(text = "
x y g
1 5 a
2 6 a
3 7 a
4 8 a
5 9 b
6 10 b
7 11 b
8 12 b", header = TRUE)

library(ggplot2)

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g) # all axes 1-12

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g, scales = "free")
# fee axes, y & y axes don't match per panel


What i want is for panel a the x and why axes both to be 1-8 and for panel b the x and y axes both to range from 5 - 12.



Is this possible?










share|improve this question























  • I wonder if you are keen to draw two separate figures and use grid.arrange(). That would be one way to go, I think.

    – jazzurro
    Feb 27 '15 at 11:04











  • @jazzurro I was thinking about employing grid.arrange although it seems a bit frustrating when you have 8-12 panels.

    – user1320502
    Feb 27 '15 at 11:10













  • I see your point. But, I'd be happy to use ncol and nrow in grid.arrange rather than thinking about how I can manipulate x and y axis for each figure.

    – jazzurro
    Feb 27 '15 at 11:20











  • Yeah i just wrote a apply to do this, I'll just find out how to match the x and y limits for a ggplot then i could use that

    – user1320502
    Feb 27 '15 at 11:28














0












0








0








I would like to make a plot using facet_wrap where the axes can vary for each panel but within a panel the x and y axes should be the same scale.



e.g. see the following plots



df <- read.table(text = "
x y g
1 5 a
2 6 a
3 7 a
4 8 a
5 9 b
6 10 b
7 11 b
8 12 b", header = TRUE)

library(ggplot2)

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g) # all axes 1-12

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g, scales = "free")
# fee axes, y & y axes don't match per panel


What i want is for panel a the x and why axes both to be 1-8 and for panel b the x and y axes both to range from 5 - 12.



Is this possible?










share|improve this question














I would like to make a plot using facet_wrap where the axes can vary for each panel but within a panel the x and y axes should be the same scale.



e.g. see the following plots



df <- read.table(text = "
x y g
1 5 a
2 6 a
3 7 a
4 8 a
5 9 b
6 10 b
7 11 b
8 12 b", header = TRUE)

library(ggplot2)

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g) # all axes 1-12

ggplot(df, aes(x=x,y=y,g=g)) +
geom_point() +
facet_wrap(~ g, scales = "free")
# fee axes, y & y axes don't match per panel


What i want is for panel a the x and why axes both to be 1-8 and for panel b the x and y axes both to range from 5 - 12.



Is this possible?







r plot ggplot2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 27 '15 at 10:53









user1320502user1320502

99021738




99021738













  • I wonder if you are keen to draw two separate figures and use grid.arrange(). That would be one way to go, I think.

    – jazzurro
    Feb 27 '15 at 11:04











  • @jazzurro I was thinking about employing grid.arrange although it seems a bit frustrating when you have 8-12 panels.

    – user1320502
    Feb 27 '15 at 11:10













  • I see your point. But, I'd be happy to use ncol and nrow in grid.arrange rather than thinking about how I can manipulate x and y axis for each figure.

    – jazzurro
    Feb 27 '15 at 11:20











  • Yeah i just wrote a apply to do this, I'll just find out how to match the x and y limits for a ggplot then i could use that

    – user1320502
    Feb 27 '15 at 11:28



















  • I wonder if you are keen to draw two separate figures and use grid.arrange(). That would be one way to go, I think.

    – jazzurro
    Feb 27 '15 at 11:04











  • @jazzurro I was thinking about employing grid.arrange although it seems a bit frustrating when you have 8-12 panels.

    – user1320502
    Feb 27 '15 at 11:10













  • I see your point. But, I'd be happy to use ncol and nrow in grid.arrange rather than thinking about how I can manipulate x and y axis for each figure.

    – jazzurro
    Feb 27 '15 at 11:20











  • Yeah i just wrote a apply to do this, I'll just find out how to match the x and y limits for a ggplot then i could use that

    – user1320502
    Feb 27 '15 at 11:28

















I wonder if you are keen to draw two separate figures and use grid.arrange(). That would be one way to go, I think.

– jazzurro
Feb 27 '15 at 11:04





I wonder if you are keen to draw two separate figures and use grid.arrange(). That would be one way to go, I think.

– jazzurro
Feb 27 '15 at 11:04













@jazzurro I was thinking about employing grid.arrange although it seems a bit frustrating when you have 8-12 panels.

– user1320502
Feb 27 '15 at 11:10







@jazzurro I was thinking about employing grid.arrange although it seems a bit frustrating when you have 8-12 panels.

– user1320502
Feb 27 '15 at 11:10















I see your point. But, I'd be happy to use ncol and nrow in grid.arrange rather than thinking about how I can manipulate x and y axis for each figure.

– jazzurro
Feb 27 '15 at 11:20





I see your point. But, I'd be happy to use ncol and nrow in grid.arrange rather than thinking about how I can manipulate x and y axis for each figure.

– jazzurro
Feb 27 '15 at 11:20













Yeah i just wrote a apply to do this, I'll just find out how to match the x and y limits for a ggplot then i could use that

– user1320502
Feb 27 '15 at 11:28





Yeah i just wrote a apply to do this, I'll just find out how to match the x and y limits for a ggplot then i could use that

– user1320502
Feb 27 '15 at 11:28












3 Answers
3






active

oldest

votes


















2














Using this answer you could try the following:



dummy <- data.frame(x = c(1, 8, 5, 12), y = c(1, 8, 5, 12), g = c("a", "a", "b", "b"))
ggplot(df, aes(x=x,y=y)) +
geom_point() +
facet_wrap(~ g, scales = "free") +
geom_blank(data = dummy)


enter image description here






share|improve this answer


























  • very good! +1 cheers, didn't know about geom_blank

    – user1320502
    Feb 27 '15 at 11:37













  • If you have more panels you just have to adapt the dummy data.

    – DatamineR
    Feb 27 '15 at 11:39



















0














This wasn't an option when the question was asked, but these days I would highly recommend patchwork for combining plots.






share|improve this answer































    0














    Another solution is trick the axes for individual facet_wrap() plots by adding invisible points to the plots with x and y reversed so that the plotted data is "square", e.g.,



    library(ggplot2)
    p <- ggplot(data = df) +
    geom_point(mapping = aes(x = x, y = y)) +
    geom_point(mapping = aes(x = y, y = x), alpha = 0) +
    facet_wrap( ~ g, scales = "free")
    print(p)


    You could also use geom_blank(). You don't need dummy data.






    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%2f28763347%2ffacet-wrap-equal-axis-per-panel%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Using this answer you could try the following:



      dummy <- data.frame(x = c(1, 8, 5, 12), y = c(1, 8, 5, 12), g = c("a", "a", "b", "b"))
      ggplot(df, aes(x=x,y=y)) +
      geom_point() +
      facet_wrap(~ g, scales = "free") +
      geom_blank(data = dummy)


      enter image description here






      share|improve this answer


























      • very good! +1 cheers, didn't know about geom_blank

        – user1320502
        Feb 27 '15 at 11:37













      • If you have more panels you just have to adapt the dummy data.

        – DatamineR
        Feb 27 '15 at 11:39
















      2














      Using this answer you could try the following:



      dummy <- data.frame(x = c(1, 8, 5, 12), y = c(1, 8, 5, 12), g = c("a", "a", "b", "b"))
      ggplot(df, aes(x=x,y=y)) +
      geom_point() +
      facet_wrap(~ g, scales = "free") +
      geom_blank(data = dummy)


      enter image description here






      share|improve this answer


























      • very good! +1 cheers, didn't know about geom_blank

        – user1320502
        Feb 27 '15 at 11:37













      • If you have more panels you just have to adapt the dummy data.

        – DatamineR
        Feb 27 '15 at 11:39














      2












      2








      2







      Using this answer you could try the following:



      dummy <- data.frame(x = c(1, 8, 5, 12), y = c(1, 8, 5, 12), g = c("a", "a", "b", "b"))
      ggplot(df, aes(x=x,y=y)) +
      geom_point() +
      facet_wrap(~ g, scales = "free") +
      geom_blank(data = dummy)


      enter image description here






      share|improve this answer















      Using this answer you could try the following:



      dummy <- data.frame(x = c(1, 8, 5, 12), y = c(1, 8, 5, 12), g = c("a", "a", "b", "b"))
      ggplot(df, aes(x=x,y=y)) +
      geom_point() +
      facet_wrap(~ g, scales = "free") +
      geom_blank(data = dummy)


      enter image description here







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 23 '17 at 12:29









      Community

      11




      11










      answered Feb 27 '15 at 11:28









      DatamineRDatamineR

      8,61111734




      8,61111734













      • very good! +1 cheers, didn't know about geom_blank

        – user1320502
        Feb 27 '15 at 11:37













      • If you have more panels you just have to adapt the dummy data.

        – DatamineR
        Feb 27 '15 at 11:39



















      • very good! +1 cheers, didn't know about geom_blank

        – user1320502
        Feb 27 '15 at 11:37













      • If you have more panels you just have to adapt the dummy data.

        – DatamineR
        Feb 27 '15 at 11:39

















      very good! +1 cheers, didn't know about geom_blank

      – user1320502
      Feb 27 '15 at 11:37







      very good! +1 cheers, didn't know about geom_blank

      – user1320502
      Feb 27 '15 at 11:37















      If you have more panels you just have to adapt the dummy data.

      – DatamineR
      Feb 27 '15 at 11:39





      If you have more panels you just have to adapt the dummy data.

      – DatamineR
      Feb 27 '15 at 11:39













      0














      This wasn't an option when the question was asked, but these days I would highly recommend patchwork for combining plots.






      share|improve this answer




























        0














        This wasn't an option when the question was asked, but these days I would highly recommend patchwork for combining plots.






        share|improve this answer


























          0












          0








          0







          This wasn't an option when the question was asked, but these days I would highly recommend patchwork for combining plots.






          share|improve this answer













          This wasn't an option when the question was asked, but these days I would highly recommend patchwork for combining plots.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 20:11









          slacklineslackline

          1,02421333




          1,02421333























              0














              Another solution is trick the axes for individual facet_wrap() plots by adding invisible points to the plots with x and y reversed so that the plotted data is "square", e.g.,



              library(ggplot2)
              p <- ggplot(data = df) +
              geom_point(mapping = aes(x = x, y = y)) +
              geom_point(mapping = aes(x = y, y = x), alpha = 0) +
              facet_wrap( ~ g, scales = "free")
              print(p)


              You could also use geom_blank(). You don't need dummy data.






              share|improve this answer






























                0














                Another solution is trick the axes for individual facet_wrap() plots by adding invisible points to the plots with x and y reversed so that the plotted data is "square", e.g.,



                library(ggplot2)
                p <- ggplot(data = df) +
                geom_point(mapping = aes(x = x, y = y)) +
                geom_point(mapping = aes(x = y, y = x), alpha = 0) +
                facet_wrap( ~ g, scales = "free")
                print(p)


                You could also use geom_blank(). You don't need dummy data.






                share|improve this answer




























                  0












                  0








                  0







                  Another solution is trick the axes for individual facet_wrap() plots by adding invisible points to the plots with x and y reversed so that the plotted data is "square", e.g.,



                  library(ggplot2)
                  p <- ggplot(data = df) +
                  geom_point(mapping = aes(x = x, y = y)) +
                  geom_point(mapping = aes(x = y, y = x), alpha = 0) +
                  facet_wrap( ~ g, scales = "free")
                  print(p)


                  You could also use geom_blank(). You don't need dummy data.






                  share|improve this answer















                  Another solution is trick the axes for individual facet_wrap() plots by adding invisible points to the plots with x and y reversed so that the plotted data is "square", e.g.,



                  library(ggplot2)
                  p <- ggplot(data = df) +
                  geom_point(mapping = aes(x = x, y = y)) +
                  geom_point(mapping = aes(x = y, y = x), alpha = 0) +
                  facet_wrap( ~ g, scales = "free")
                  print(p)


                  You could also use geom_blank(). You don't need dummy data.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 20:50

























                  answered Nov 23 '18 at 20:04









                  Simon WoodwardSimon Woodward

                  420312




                  420312






























                      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%2f28763347%2ffacet-wrap-equal-axis-per-panel%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

                      Paul Cézanne

                      UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                      Angular material date-picker (MatDatepicker) auto completes the date on focus out