Looping over variables in ggplot to create a grid of density distributions for each variable












0















I want to create a grid of density distribution plots, with a dashed vertical line at the mean, for multiple variables I have in a dataset. Using mtcars dataset as an example, the code for a single variable plot would be:



ggplot(mtcars, aes(x = mpg)) + geom_density() + geom_vline(aes(xintercept = 
mean(mpg)), linetype = "dashed", size = 0.6)


I am unclear about how I alter this to make it loop over specified variables in my dataset and produce a grid with the plots of each one. It seems like it would involve some combination of adding facet_grid and the "vars" argument but I have tried a number of combinations with no success.



It seems like in all the examples I can find online, facet_grid splits the plots by subsets of a variable, while keeping the same x and y for each plot, but I want to have the plot of x vary in each graph and the y is the density of values.



In trying to solve this, it is also my understanding that the new release of ggplot includes something involving "quasiquotation" which may help solve my problem (https://www.tidyverse.org/articles/2018/07/ggplot2-tidy-evaluation/) but again, I couldn't quite figure out how to apply the examples provided here to my own issue.










share|improve this question


















  • 2





    The examples you've found with facet_grid are all on point, you're just missing the fact that you need to transform your data slightly to make it work. You'll use something like gather from tidyr to convert a set of variables (columns) into a single pair of columns: a key and a value column. Then you'll facet on the key variable, and the value variable is your new y variable.

    – joran
    Nov 26 '18 at 17:01











  • (Sorry, minor typo, I meant "new x variable", since you're plotting densities.)

    – joran
    Nov 26 '18 at 17:09











  • Wow, that seems to do the trick! Just for anyone new to R like me who wants to see the solution, I did this to get two plots for mtcars variables disp and hp: mt_density <- mtcars %>% gather(disp, hp, key = "variable", value = "value") ggplot(mt_density, aes(x = value)) + geom_density() + geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) + facet_grid(. ~ variable)

    – Ella Wind
    Nov 26 '18 at 17:56


















0















I want to create a grid of density distribution plots, with a dashed vertical line at the mean, for multiple variables I have in a dataset. Using mtcars dataset as an example, the code for a single variable plot would be:



ggplot(mtcars, aes(x = mpg)) + geom_density() + geom_vline(aes(xintercept = 
mean(mpg)), linetype = "dashed", size = 0.6)


I am unclear about how I alter this to make it loop over specified variables in my dataset and produce a grid with the plots of each one. It seems like it would involve some combination of adding facet_grid and the "vars" argument but I have tried a number of combinations with no success.



It seems like in all the examples I can find online, facet_grid splits the plots by subsets of a variable, while keeping the same x and y for each plot, but I want to have the plot of x vary in each graph and the y is the density of values.



In trying to solve this, it is also my understanding that the new release of ggplot includes something involving "quasiquotation" which may help solve my problem (https://www.tidyverse.org/articles/2018/07/ggplot2-tidy-evaluation/) but again, I couldn't quite figure out how to apply the examples provided here to my own issue.










share|improve this question


















  • 2





    The examples you've found with facet_grid are all on point, you're just missing the fact that you need to transform your data slightly to make it work. You'll use something like gather from tidyr to convert a set of variables (columns) into a single pair of columns: a key and a value column. Then you'll facet on the key variable, and the value variable is your new y variable.

    – joran
    Nov 26 '18 at 17:01











  • (Sorry, minor typo, I meant "new x variable", since you're plotting densities.)

    – joran
    Nov 26 '18 at 17:09











  • Wow, that seems to do the trick! Just for anyone new to R like me who wants to see the solution, I did this to get two plots for mtcars variables disp and hp: mt_density <- mtcars %>% gather(disp, hp, key = "variable", value = "value") ggplot(mt_density, aes(x = value)) + geom_density() + geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) + facet_grid(. ~ variable)

    – Ella Wind
    Nov 26 '18 at 17:56
















0












0








0


1






I want to create a grid of density distribution plots, with a dashed vertical line at the mean, for multiple variables I have in a dataset. Using mtcars dataset as an example, the code for a single variable plot would be:



ggplot(mtcars, aes(x = mpg)) + geom_density() + geom_vline(aes(xintercept = 
mean(mpg)), linetype = "dashed", size = 0.6)


I am unclear about how I alter this to make it loop over specified variables in my dataset and produce a grid with the plots of each one. It seems like it would involve some combination of adding facet_grid and the "vars" argument but I have tried a number of combinations with no success.



It seems like in all the examples I can find online, facet_grid splits the plots by subsets of a variable, while keeping the same x and y for each plot, but I want to have the plot of x vary in each graph and the y is the density of values.



In trying to solve this, it is also my understanding that the new release of ggplot includes something involving "quasiquotation" which may help solve my problem (https://www.tidyverse.org/articles/2018/07/ggplot2-tidy-evaluation/) but again, I couldn't quite figure out how to apply the examples provided here to my own issue.










share|improve this question














I want to create a grid of density distribution plots, with a dashed vertical line at the mean, for multiple variables I have in a dataset. Using mtcars dataset as an example, the code for a single variable plot would be:



ggplot(mtcars, aes(x = mpg)) + geom_density() + geom_vline(aes(xintercept = 
mean(mpg)), linetype = "dashed", size = 0.6)


I am unclear about how I alter this to make it loop over specified variables in my dataset and produce a grid with the plots of each one. It seems like it would involve some combination of adding facet_grid and the "vars" argument but I have tried a number of combinations with no success.



It seems like in all the examples I can find online, facet_grid splits the plots by subsets of a variable, while keeping the same x and y for each plot, but I want to have the plot of x vary in each graph and the y is the density of values.



In trying to solve this, it is also my understanding that the new release of ggplot includes something involving "quasiquotation" which may help solve my problem (https://www.tidyverse.org/articles/2018/07/ggplot2-tidy-evaluation/) but again, I couldn't quite figure out how to apply the examples provided here to my own issue.







r loops ggplot2 facet-grid






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 16:57









Ella WindElla Wind

155




155








  • 2





    The examples you've found with facet_grid are all on point, you're just missing the fact that you need to transform your data slightly to make it work. You'll use something like gather from tidyr to convert a set of variables (columns) into a single pair of columns: a key and a value column. Then you'll facet on the key variable, and the value variable is your new y variable.

    – joran
    Nov 26 '18 at 17:01











  • (Sorry, minor typo, I meant "new x variable", since you're plotting densities.)

    – joran
    Nov 26 '18 at 17:09











  • Wow, that seems to do the trick! Just for anyone new to R like me who wants to see the solution, I did this to get two plots for mtcars variables disp and hp: mt_density <- mtcars %>% gather(disp, hp, key = "variable", value = "value") ggplot(mt_density, aes(x = value)) + geom_density() + geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) + facet_grid(. ~ variable)

    – Ella Wind
    Nov 26 '18 at 17:56
















  • 2





    The examples you've found with facet_grid are all on point, you're just missing the fact that you need to transform your data slightly to make it work. You'll use something like gather from tidyr to convert a set of variables (columns) into a single pair of columns: a key and a value column. Then you'll facet on the key variable, and the value variable is your new y variable.

    – joran
    Nov 26 '18 at 17:01











  • (Sorry, minor typo, I meant "new x variable", since you're plotting densities.)

    – joran
    Nov 26 '18 at 17:09











  • Wow, that seems to do the trick! Just for anyone new to R like me who wants to see the solution, I did this to get two plots for mtcars variables disp and hp: mt_density <- mtcars %>% gather(disp, hp, key = "variable", value = "value") ggplot(mt_density, aes(x = value)) + geom_density() + geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) + facet_grid(. ~ variable)

    – Ella Wind
    Nov 26 '18 at 17:56










2




2





The examples you've found with facet_grid are all on point, you're just missing the fact that you need to transform your data slightly to make it work. You'll use something like gather from tidyr to convert a set of variables (columns) into a single pair of columns: a key and a value column. Then you'll facet on the key variable, and the value variable is your new y variable.

– joran
Nov 26 '18 at 17:01





The examples you've found with facet_grid are all on point, you're just missing the fact that you need to transform your data slightly to make it work. You'll use something like gather from tidyr to convert a set of variables (columns) into a single pair of columns: a key and a value column. Then you'll facet on the key variable, and the value variable is your new y variable.

– joran
Nov 26 '18 at 17:01













(Sorry, minor typo, I meant "new x variable", since you're plotting densities.)

– joran
Nov 26 '18 at 17:09





(Sorry, minor typo, I meant "new x variable", since you're plotting densities.)

– joran
Nov 26 '18 at 17:09













Wow, that seems to do the trick! Just for anyone new to R like me who wants to see the solution, I did this to get two plots for mtcars variables disp and hp: mt_density <- mtcars %>% gather(disp, hp, key = "variable", value = "value") ggplot(mt_density, aes(x = value)) + geom_density() + geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) + facet_grid(. ~ variable)

– Ella Wind
Nov 26 '18 at 17:56







Wow, that seems to do the trick! Just for anyone new to R like me who wants to see the solution, I did this to get two plots for mtcars variables disp and hp: mt_density <- mtcars %>% gather(disp, hp, key = "variable", value = "value") ggplot(mt_density, aes(x = value)) + geom_density() + geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) + facet_grid(. ~ variable)

– Ella Wind
Nov 26 '18 at 17:56














1 Answer
1






active

oldest

votes


















1














Consider reshaping the data into long format than plotting with facets. Here both x and y scales are free since plot differ in magnitude across the columns.



rdf <- reshape(mtcars, varying = names(mtcars), v.names = "value", 
times = names(mtcars), timevar = "variable",
new.row.names = 1:1000, direction = "long")

ggplot(rdf, aes(x = value)) + geom_density() +
geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) +
facet_grid(~variable, scales="free")


Plot Output






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%2f53485763%2flooping-over-variables-in-ggplot-to-create-a-grid-of-density-distributions-for-e%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














    Consider reshaping the data into long format than plotting with facets. Here both x and y scales are free since plot differ in magnitude across the columns.



    rdf <- reshape(mtcars, varying = names(mtcars), v.names = "value", 
    times = names(mtcars), timevar = "variable",
    new.row.names = 1:1000, direction = "long")

    ggplot(rdf, aes(x = value)) + geom_density() +
    geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) +
    facet_grid(~variable, scales="free")


    Plot Output






    share|improve this answer




























      1














      Consider reshaping the data into long format than plotting with facets. Here both x and y scales are free since plot differ in magnitude across the columns.



      rdf <- reshape(mtcars, varying = names(mtcars), v.names = "value", 
      times = names(mtcars), timevar = "variable",
      new.row.names = 1:1000, direction = "long")

      ggplot(rdf, aes(x = value)) + geom_density() +
      geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) +
      facet_grid(~variable, scales="free")


      Plot Output






      share|improve this answer


























        1












        1








        1







        Consider reshaping the data into long format than plotting with facets. Here both x and y scales are free since plot differ in magnitude across the columns.



        rdf <- reshape(mtcars, varying = names(mtcars), v.names = "value", 
        times = names(mtcars), timevar = "variable",
        new.row.names = 1:1000, direction = "long")

        ggplot(rdf, aes(x = value)) + geom_density() +
        geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) +
        facet_grid(~variable, scales="free")


        Plot Output






        share|improve this answer













        Consider reshaping the data into long format than plotting with facets. Here both x and y scales are free since plot differ in magnitude across the columns.



        rdf <- reshape(mtcars, varying = names(mtcars), v.names = "value", 
        times = names(mtcars), timevar = "variable",
        new.row.names = 1:1000, direction = "long")

        ggplot(rdf, aes(x = value)) + geom_density() +
        geom_vline(aes(xintercept = mean(value)), linetype = "dashed", size = 0.6) +
        facet_grid(~variable, scales="free")


        Plot Output







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 17:56









        ParfaitParfait

        51.8k84470




        51.8k84470
































            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%2f53485763%2flooping-over-variables-in-ggplot-to-create-a-grid-of-density-distributions-for-e%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

            Lallio

            Futebolista

            Jornalista