Multiple qqplots on one gragh and single abline ggplot2 R












1















Borrowing from the example QQ plot: More than two data, how can one possibly implement it in ggplot2? I need one abline only.



Sample data:



set.seed(10)
dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))


sim1...sim6 should have different colors.



All suggestions are welcome.










share|improve this question



























    1















    Borrowing from the example QQ plot: More than two data, how can one possibly implement it in ggplot2? I need one abline only.



    Sample data:



    set.seed(10)
    dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))


    sim1...sim6 should have different colors.



    All suggestions are welcome.










    share|improve this question

























      1












      1








      1








      Borrowing from the example QQ plot: More than two data, how can one possibly implement it in ggplot2? I need one abline only.



      Sample data:



      set.seed(10)
      dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))


      sim1...sim6 should have different colors.



      All suggestions are welcome.










      share|improve this question














      Borrowing from the example QQ plot: More than two data, how can one possibly implement it in ggplot2? I need one abline only.



      Sample data:



      set.seed(10)
      dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))


      sim1...sim6 should have different colors.



      All suggestions are welcome.







      r ggplot2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 20:26









      code123code123

      7481629




      7481629
























          1 Answer
          1






          active

          oldest

          votes


















          5














          Straightforward in ggplot2 with stat_qq and reshaping your data from wide to long.





          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot <- dat %>%
          gather(variable, value, -Observed) %>%
          ggplot(aes(sample = value, color = variable)) +
          geom_abline() +
          stat_qq()

          # All in one
          plot




          It might be beneficial if you look at making facets or small multiples along your comparison variable.



          # Facets!
          plot +
          facet_wrap(~variable)




          If you're looking to provide your own observed, then rather than being fancy, let qqplot do the heavy lifting but set plot.it = FALSE and it will return you a list of x/y coords for the qq plot. A little iteration with purrr::map_dfr, and you can do:



          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1 = rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot_data <- map_dfr(names(dat)[-1], ~as_tibble(qqplot(dat[[.x]], dat$Observed, plot.it = FALSE)) %>%
          mutate(id = .x))

          ggplot(plot_data, aes(x, y, color = id)) +
          geom_point() +
          geom_abline() +
          facet_wrap(~id)




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






          share|improve this answer


























          • Awesome! Thank you @Jake Kaupp

            – code123
            Nov 23 '18 at 20:49











          • stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

            – code123
            Nov 25 '18 at 18:44











          • This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

            – Jake Kaupp
            Nov 25 '18 at 21:33











          • I guess an example with the sample data will shed more light on the problem better?

            – code123
            Nov 25 '18 at 21:43






          • 1





            Updated the answer to address providing your own observed values.

            – Jake Kaupp
            Nov 26 '18 at 1:12











          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%2f53452588%2fmultiple-qqplots-on-one-gragh-and-single-abline-ggplot2-r%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









          5














          Straightforward in ggplot2 with stat_qq and reshaping your data from wide to long.





          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot <- dat %>%
          gather(variable, value, -Observed) %>%
          ggplot(aes(sample = value, color = variable)) +
          geom_abline() +
          stat_qq()

          # All in one
          plot




          It might be beneficial if you look at making facets or small multiples along your comparison variable.



          # Facets!
          plot +
          facet_wrap(~variable)




          If you're looking to provide your own observed, then rather than being fancy, let qqplot do the heavy lifting but set plot.it = FALSE and it will return you a list of x/y coords for the qq plot. A little iteration with purrr::map_dfr, and you can do:



          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1 = rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot_data <- map_dfr(names(dat)[-1], ~as_tibble(qqplot(dat[[.x]], dat$Observed, plot.it = FALSE)) %>%
          mutate(id = .x))

          ggplot(plot_data, aes(x, y, color = id)) +
          geom_point() +
          geom_abline() +
          facet_wrap(~id)




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






          share|improve this answer


























          • Awesome! Thank you @Jake Kaupp

            – code123
            Nov 23 '18 at 20:49











          • stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

            – code123
            Nov 25 '18 at 18:44











          • This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

            – Jake Kaupp
            Nov 25 '18 at 21:33











          • I guess an example with the sample data will shed more light on the problem better?

            – code123
            Nov 25 '18 at 21:43






          • 1





            Updated the answer to address providing your own observed values.

            – Jake Kaupp
            Nov 26 '18 at 1:12
















          5














          Straightforward in ggplot2 with stat_qq and reshaping your data from wide to long.





          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot <- dat %>%
          gather(variable, value, -Observed) %>%
          ggplot(aes(sample = value, color = variable)) +
          geom_abline() +
          stat_qq()

          # All in one
          plot




          It might be beneficial if you look at making facets or small multiples along your comparison variable.



          # Facets!
          plot +
          facet_wrap(~variable)




          If you're looking to provide your own observed, then rather than being fancy, let qqplot do the heavy lifting but set plot.it = FALSE and it will return you a list of x/y coords for the qq plot. A little iteration with purrr::map_dfr, and you can do:



          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1 = rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot_data <- map_dfr(names(dat)[-1], ~as_tibble(qqplot(dat[[.x]], dat$Observed, plot.it = FALSE)) %>%
          mutate(id = .x))

          ggplot(plot_data, aes(x, y, color = id)) +
          geom_point() +
          geom_abline() +
          facet_wrap(~id)




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






          share|improve this answer


























          • Awesome! Thank you @Jake Kaupp

            – code123
            Nov 23 '18 at 20:49











          • stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

            – code123
            Nov 25 '18 at 18:44











          • This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

            – Jake Kaupp
            Nov 25 '18 at 21:33











          • I guess an example with the sample data will shed more light on the problem better?

            – code123
            Nov 25 '18 at 21:43






          • 1





            Updated the answer to address providing your own observed values.

            – Jake Kaupp
            Nov 26 '18 at 1:12














          5












          5








          5







          Straightforward in ggplot2 with stat_qq and reshaping your data from wide to long.





          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot <- dat %>%
          gather(variable, value, -Observed) %>%
          ggplot(aes(sample = value, color = variable)) +
          geom_abline() +
          stat_qq()

          # All in one
          plot




          It might be beneficial if you look at making facets or small multiples along your comparison variable.



          # Facets!
          plot +
          facet_wrap(~variable)




          If you're looking to provide your own observed, then rather than being fancy, let qqplot do the heavy lifting but set plot.it = FALSE and it will return you a list of x/y coords for the qq plot. A little iteration with purrr::map_dfr, and you can do:



          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1 = rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot_data <- map_dfr(names(dat)[-1], ~as_tibble(qqplot(dat[[.x]], dat$Observed, plot.it = FALSE)) %>%
          mutate(id = .x))

          ggplot(plot_data, aes(x, y, color = id)) +
          geom_point() +
          geom_abline() +
          facet_wrap(~id)




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






          share|improve this answer















          Straightforward in ggplot2 with stat_qq and reshaping your data from wide to long.





          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot <- dat %>%
          gather(variable, value, -Observed) %>%
          ggplot(aes(sample = value, color = variable)) +
          geom_abline() +
          stat_qq()

          # All in one
          plot




          It might be beneficial if you look at making facets or small multiples along your comparison variable.



          # Facets!
          plot +
          facet_wrap(~variable)




          If you're looking to provide your own observed, then rather than being fancy, let qqplot do the heavy lifting but set plot.it = FALSE and it will return you a list of x/y coords for the qq plot. A little iteration with purrr::map_dfr, and you can do:



          library(tidyverse)

          set.seed(10)
          dat <- data.frame(Observed = rnorm(20), sim1 = rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))

          plot_data <- map_dfr(names(dat)[-1], ~as_tibble(qqplot(dat[[.x]], dat$Observed, plot.it = FALSE)) %>%
          mutate(id = .x))

          ggplot(plot_data, aes(x, y, color = id)) +
          geom_point() +
          geom_abline() +
          facet_wrap(~id)




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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 1:11

























          answered Nov 23 '18 at 20:44









          Jake KauppJake Kaupp

          5,64721428




          5,64721428













          • Awesome! Thank you @Jake Kaupp

            – code123
            Nov 23 '18 at 20:49











          • stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

            – code123
            Nov 25 '18 at 18:44











          • This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

            – Jake Kaupp
            Nov 25 '18 at 21:33











          • I guess an example with the sample data will shed more light on the problem better?

            – code123
            Nov 25 '18 at 21:43






          • 1





            Updated the answer to address providing your own observed values.

            – Jake Kaupp
            Nov 26 '18 at 1:12



















          • Awesome! Thank you @Jake Kaupp

            – code123
            Nov 23 '18 at 20:49











          • stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

            – code123
            Nov 25 '18 at 18:44











          • This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

            – Jake Kaupp
            Nov 25 '18 at 21:33











          • I guess an example with the sample data will shed more light on the problem better?

            – code123
            Nov 25 '18 at 21:43






          • 1





            Updated the answer to address providing your own observed values.

            – Jake Kaupp
            Nov 26 '18 at 1:12

















          Awesome! Thank you @Jake Kaupp

          – code123
          Nov 23 '18 at 20:49





          Awesome! Thank you @Jake Kaupp

          – code123
          Nov 23 '18 at 20:49













          stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

          – code123
          Nov 25 '18 at 18:44





          stat_qq and geom_qq seem to seem to be designed to compare one or more vectors e.g. sim1-6 vector to a theoretical distribution, I couldn't see how to use it to compare two different vectors, e.g each of sim1 to sim6 compared against Observed? How can I do exactly what I specified in the link under my question but in ggplot2? Thank you.

          – code123
          Nov 25 '18 at 18:44













          This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

          – Jake Kaupp
          Nov 25 '18 at 21:33





          This is where you would conduct your comparison outside of ggplot, then just use geom_point to plot your expected vs observed.

          – Jake Kaupp
          Nov 25 '18 at 21:33













          I guess an example with the sample data will shed more light on the problem better?

          – code123
          Nov 25 '18 at 21:43





          I guess an example with the sample data will shed more light on the problem better?

          – code123
          Nov 25 '18 at 21:43




          1




          1





          Updated the answer to address providing your own observed values.

          – Jake Kaupp
          Nov 26 '18 at 1:12





          Updated the answer to address providing your own observed values.

          – Jake Kaupp
          Nov 26 '18 at 1:12




















          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%2f53452588%2fmultiple-qqplots-on-one-gragh-and-single-abline-ggplot2-r%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

          Wiesbaden

          Marschland

          Dieringhausen