Maintain ggplot panel size while axis labels change length












0














How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.



library(dplyr)
library(ggplot2)

ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)

plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_short
plot_long


enter image description hereenter image description here



For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?










share|improve this question






















  • Is using facets not an option?
    – Maurits Evers
    Nov 13 at 5:54










  • @MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
    – Joe
    Nov 13 at 6:02










  • Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
    – Henrik
    Nov 13 at 7:32


















0














How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.



library(dplyr)
library(ggplot2)

ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)

plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_short
plot_long


enter image description hereenter image description here



For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?










share|improve this question






















  • Is using facets not an option?
    – Maurits Evers
    Nov 13 at 5:54










  • @MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
    – Joe
    Nov 13 at 6:02










  • Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
    – Henrik
    Nov 13 at 7:32
















0












0








0







How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.



library(dplyr)
library(ggplot2)

ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)

plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_short
plot_long


enter image description hereenter image description here



For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?










share|improve this question













How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.



library(dplyr)
library(ggplot2)

ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)

plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()

plot_short
plot_long


enter image description hereenter image description here



For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?







r ggplot2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 5:37









Joe

683514




683514












  • Is using facets not an option?
    – Maurits Evers
    Nov 13 at 5:54










  • @MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
    – Joe
    Nov 13 at 6:02










  • Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
    – Henrik
    Nov 13 at 7:32




















  • Is using facets not an option?
    – Maurits Evers
    Nov 13 at 5:54










  • @MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
    – Joe
    Nov 13 at 6:02










  • Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
    – Henrik
    Nov 13 at 7:32


















Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54




Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54












@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02




@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02












Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32






Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32














2 Answers
2






active

oldest

votes


















1














We can use gridarrange from the egg package



library(egg)
ggarrange(plot_short, plot_long, ncol = 1)


enter image description here



To save, use



gg <- ggarrange(plot_short, plot_long, ncol = 1)
ggsave("file.png", gg)





share|improve this answer





























    1














    try egg::set_panel_size(plot_short)






    share|improve this answer





















    • Please improve your question little bit more and also demonstrate it
      – Ved Prakash
      Nov 13 at 5:54










    • @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
      – Joe
      Nov 13 at 16:33











    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%2f53274483%2fmaintain-ggplot-panel-size-while-axis-labels-change-length%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    We can use gridarrange from the egg package



    library(egg)
    ggarrange(plot_short, plot_long, ncol = 1)


    enter image description here



    To save, use



    gg <- ggarrange(plot_short, plot_long, ncol = 1)
    ggsave("file.png", gg)





    share|improve this answer


























      1














      We can use gridarrange from the egg package



      library(egg)
      ggarrange(plot_short, plot_long, ncol = 1)


      enter image description here



      To save, use



      gg <- ggarrange(plot_short, plot_long, ncol = 1)
      ggsave("file.png", gg)





      share|improve this answer
























        1












        1








        1






        We can use gridarrange from the egg package



        library(egg)
        ggarrange(plot_short, plot_long, ncol = 1)


        enter image description here



        To save, use



        gg <- ggarrange(plot_short, plot_long, ncol = 1)
        ggsave("file.png", gg)





        share|improve this answer












        We can use gridarrange from the egg package



        library(egg)
        ggarrange(plot_short, plot_long, ncol = 1)


        enter image description here



        To save, use



        gg <- ggarrange(plot_short, plot_long, ncol = 1)
        ggsave("file.png", gg)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 6:16









        Maurits Evers

        25.8k41532




        25.8k41532

























            1














            try egg::set_panel_size(plot_short)






            share|improve this answer





















            • Please improve your question little bit more and also demonstrate it
              – Ved Prakash
              Nov 13 at 5:54










            • @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
              – Joe
              Nov 13 at 16:33
















            1














            try egg::set_panel_size(plot_short)






            share|improve this answer





















            • Please improve your question little bit more and also demonstrate it
              – Ved Prakash
              Nov 13 at 5:54










            • @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
              – Joe
              Nov 13 at 16:33














            1












            1








            1






            try egg::set_panel_size(plot_short)






            share|improve this answer












            try egg::set_panel_size(plot_short)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 at 5:45









            user10644264

            111




            111












            • Please improve your question little bit more and also demonstrate it
              – Ved Prakash
              Nov 13 at 5:54










            • @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
              – Joe
              Nov 13 at 16:33


















            • Please improve your question little bit more and also demonstrate it
              – Ved Prakash
              Nov 13 at 5:54










            • @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
              – Joe
              Nov 13 at 16:33
















            Please improve your question little bit more and also demonstrate it
            – Ved Prakash
            Nov 13 at 5:54




            Please improve your question little bit more and also demonstrate it
            – Ved Prakash
            Nov 13 at 5:54












            @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
            – Joe
            Nov 13 at 16:33




            @user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
            – Joe
            Nov 13 at 16:33


















            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%2f53274483%2fmaintain-ggplot-panel-size-while-axis-labels-change-length%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)