plot grouped data in R












3















I have a grouped data as follows:



group   x   y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5


both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?










share|improve this question





























    3















    I have a grouped data as follows:



    group   x   y
    group1 0 5
    group4 0 5
    group1 7 5
    group4 0 5
    group5 7 5
    group1 7 5
    group1 0 6
    group2 0 6
    group4 0 5
    group2 0 5
    group3 7 5


    both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?










    share|improve this question



























      3












      3








      3








      I have a grouped data as follows:



      group   x   y
      group1 0 5
      group4 0 5
      group1 7 5
      group4 0 5
      group5 7 5
      group1 7 5
      group1 0 6
      group2 0 6
      group4 0 5
      group2 0 5
      group3 7 5


      both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?










      share|improve this question
















      I have a grouped data as follows:



      group   x   y
      group1 0 5
      group4 0 5
      group1 7 5
      group4 0 5
      group5 7 5
      group1 7 5
      group1 0 6
      group2 0 6
      group4 0 5
      group2 0 5
      group3 7 5


      both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?







      r graph plot






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 21 '13 at 12:17









      Arun

      93.6k11218319




      93.6k11218319










      asked Mar 21 '13 at 11:38









      user297850user297850

      2,593103450




      2,593103450
























          3 Answers
          3






          active

          oldest

          votes


















          4














          The data:



          dat <- read.table(text = "group   x   y
          group1 0 5
          group4 0 5
          group1 7 5
          group4 0 5
          group5 7 5
          group1 7 5
          group1 0 6
          group2 0 6
          group4 0 5
          group2 0 5
          group3 7 5", header = TRUE)


          You can use the excellent ggplot2 package for easy plotting:



          library(ggplot2)
          ggplot(dat, aes(x = x, y = y, colour = group)) +
          geom_point() +
          facet_wrap( ~ group)


          Here, I used facet_wrap to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.



          enter image description here






          share|improve this answer































            4














            Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:



            library(lattice)
            # Each group in a separate mini plot
            xyplot(y ~ x | group, data = dat)
            # All groups in one plot, different colors for each group
            # Not at all interesting with the example data you've provided
            xyplot(y ~ x, groups=dat$group, data = dat)


            Here's an example of each with a little bit more data:



            set.seed(1)
            mydf <- data.frame(
            group = sample(letters[1:4], 50, replace = TRUE),
            x = runif(50, 0, 7),
            y = runif(50, 0, 7)
            )
            xyplot(y ~ x, groups=mydf$group, data = mydf,
            auto.key = list(corner = c(0, .98)), cex = 1.5)


            enter image description here



            xyplot(y ~ x | group, data = mydf, 
            auto.key = list(corner = c(0, .98)), cex = 1.5)


            enter image description here






            share|improve this answer

































              0














              dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
              g <- ggplot(dd, aes(as.factor(group)))
              g <- g + geom_point(aes(y=x), colour="red")
              g <- g + geom_point(aes(y=y), colour="green")
              g


              This would give you x and y as a function of the group, in a single plot, as shown below. plotting variables as function of group






              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%2f15546468%2fplot-grouped-data-in-r%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









                4














                The data:



                dat <- read.table(text = "group   x   y
                group1 0 5
                group4 0 5
                group1 7 5
                group4 0 5
                group5 7 5
                group1 7 5
                group1 0 6
                group2 0 6
                group4 0 5
                group2 0 5
                group3 7 5", header = TRUE)


                You can use the excellent ggplot2 package for easy plotting:



                library(ggplot2)
                ggplot(dat, aes(x = x, y = y, colour = group)) +
                geom_point() +
                facet_wrap( ~ group)


                Here, I used facet_wrap to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.



                enter image description here






                share|improve this answer




























                  4














                  The data:



                  dat <- read.table(text = "group   x   y
                  group1 0 5
                  group4 0 5
                  group1 7 5
                  group4 0 5
                  group5 7 5
                  group1 7 5
                  group1 0 6
                  group2 0 6
                  group4 0 5
                  group2 0 5
                  group3 7 5", header = TRUE)


                  You can use the excellent ggplot2 package for easy plotting:



                  library(ggplot2)
                  ggplot(dat, aes(x = x, y = y, colour = group)) +
                  geom_point() +
                  facet_wrap( ~ group)


                  Here, I used facet_wrap to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.



                  enter image description here






                  share|improve this answer


























                    4












                    4








                    4







                    The data:



                    dat <- read.table(text = "group   x   y
                    group1 0 5
                    group4 0 5
                    group1 7 5
                    group4 0 5
                    group5 7 5
                    group1 7 5
                    group1 0 6
                    group2 0 6
                    group4 0 5
                    group2 0 5
                    group3 7 5", header = TRUE)


                    You can use the excellent ggplot2 package for easy plotting:



                    library(ggplot2)
                    ggplot(dat, aes(x = x, y = y, colour = group)) +
                    geom_point() +
                    facet_wrap( ~ group)


                    Here, I used facet_wrap to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.



                    enter image description here






                    share|improve this answer













                    The data:



                    dat <- read.table(text = "group   x   y
                    group1 0 5
                    group4 0 5
                    group1 7 5
                    group4 0 5
                    group5 7 5
                    group1 7 5
                    group1 0 6
                    group2 0 6
                    group4 0 5
                    group2 0 5
                    group3 7 5", header = TRUE)


                    You can use the excellent ggplot2 package for easy plotting:



                    library(ggplot2)
                    ggplot(dat, aes(x = x, y = y, colour = group)) +
                    geom_point() +
                    facet_wrap( ~ group)


                    Here, I used facet_wrap to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.



                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 21 '13 at 11:47









                    Sven HohensteinSven Hohenstein

                    66.1k12100131




                    66.1k12100131

























                        4














                        Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:



                        library(lattice)
                        # Each group in a separate mini plot
                        xyplot(y ~ x | group, data = dat)
                        # All groups in one plot, different colors for each group
                        # Not at all interesting with the example data you've provided
                        xyplot(y ~ x, groups=dat$group, data = dat)


                        Here's an example of each with a little bit more data:



                        set.seed(1)
                        mydf <- data.frame(
                        group = sample(letters[1:4], 50, replace = TRUE),
                        x = runif(50, 0, 7),
                        y = runif(50, 0, 7)
                        )
                        xyplot(y ~ x, groups=mydf$group, data = mydf,
                        auto.key = list(corner = c(0, .98)), cex = 1.5)


                        enter image description here



                        xyplot(y ~ x | group, data = mydf, 
                        auto.key = list(corner = c(0, .98)), cex = 1.5)


                        enter image description here






                        share|improve this answer






























                          4














                          Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:



                          library(lattice)
                          # Each group in a separate mini plot
                          xyplot(y ~ x | group, data = dat)
                          # All groups in one plot, different colors for each group
                          # Not at all interesting with the example data you've provided
                          xyplot(y ~ x, groups=dat$group, data = dat)


                          Here's an example of each with a little bit more data:



                          set.seed(1)
                          mydf <- data.frame(
                          group = sample(letters[1:4], 50, replace = TRUE),
                          x = runif(50, 0, 7),
                          y = runif(50, 0, 7)
                          )
                          xyplot(y ~ x, groups=mydf$group, data = mydf,
                          auto.key = list(corner = c(0, .98)), cex = 1.5)


                          enter image description here



                          xyplot(y ~ x | group, data = mydf, 
                          auto.key = list(corner = c(0, .98)), cex = 1.5)


                          enter image description here






                          share|improve this answer




























                            4












                            4








                            4







                            Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:



                            library(lattice)
                            # Each group in a separate mini plot
                            xyplot(y ~ x | group, data = dat)
                            # All groups in one plot, different colors for each group
                            # Not at all interesting with the example data you've provided
                            xyplot(y ~ x, groups=dat$group, data = dat)


                            Here's an example of each with a little bit more data:



                            set.seed(1)
                            mydf <- data.frame(
                            group = sample(letters[1:4], 50, replace = TRUE),
                            x = runif(50, 0, 7),
                            y = runif(50, 0, 7)
                            )
                            xyplot(y ~ x, groups=mydf$group, data = mydf,
                            auto.key = list(corner = c(0, .98)), cex = 1.5)


                            enter image description here



                            xyplot(y ~ x | group, data = mydf, 
                            auto.key = list(corner = c(0, .98)), cex = 1.5)


                            enter image description here






                            share|improve this answer















                            Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:



                            library(lattice)
                            # Each group in a separate mini plot
                            xyplot(y ~ x | group, data = dat)
                            # All groups in one plot, different colors for each group
                            # Not at all interesting with the example data you've provided
                            xyplot(y ~ x, groups=dat$group, data = dat)


                            Here's an example of each with a little bit more data:



                            set.seed(1)
                            mydf <- data.frame(
                            group = sample(letters[1:4], 50, replace = TRUE),
                            x = runif(50, 0, 7),
                            y = runif(50, 0, 7)
                            )
                            xyplot(y ~ x, groups=mydf$group, data = mydf,
                            auto.key = list(corner = c(0, .98)), cex = 1.5)


                            enter image description here



                            xyplot(y ~ x | group, data = mydf, 
                            auto.key = list(corner = c(0, .98)), cex = 1.5)


                            enter image description here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 21 '13 at 12:13

























                            answered Mar 21 '13 at 11:55









                            A5C1D2H2I1M1N2O1R2T1A5C1D2H2I1M1N2O1R2T1

                            154k18287381




                            154k18287381























                                0














                                dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
                                g <- ggplot(dd, aes(as.factor(group)))
                                g <- g + geom_point(aes(y=x), colour="red")
                                g <- g + geom_point(aes(y=y), colour="green")
                                g


                                This would give you x and y as a function of the group, in a single plot, as shown below. plotting variables as function of group






                                share|improve this answer




























                                  0














                                  dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
                                  g <- ggplot(dd, aes(as.factor(group)))
                                  g <- g + geom_point(aes(y=x), colour="red")
                                  g <- g + geom_point(aes(y=y), colour="green")
                                  g


                                  This would give you x and y as a function of the group, in a single plot, as shown below. plotting variables as function of group






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
                                    g <- ggplot(dd, aes(as.factor(group)))
                                    g <- g + geom_point(aes(y=x), colour="red")
                                    g <- g + geom_point(aes(y=y), colour="green")
                                    g


                                    This would give you x and y as a function of the group, in a single plot, as shown below. plotting variables as function of group






                                    share|improve this answer













                                    dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
                                    g <- ggplot(dd, aes(as.factor(group)))
                                    g <- g + geom_point(aes(y=x), colour="red")
                                    g <- g + geom_point(aes(y=y), colour="green")
                                    g


                                    This would give you x and y as a function of the group, in a single plot, as shown below. plotting variables as function of group







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 22 '13 at 11:36









                                    Venkatramanan P.R.Venkatramanan P.R.

                                    843




                                    843






























                                        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%2f15546468%2fplot-grouped-data-in-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