ggplot2: Combining group, color and linetype












8














I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.



As an exemple, my data looks like this:



DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8)

> DT
condition measure time score
1 control A pre-test 1
2 control A post-test 2
3 control B pre-test 3
4 control B post-test 4
5 experimental A pre-test 5
6 experimental A post-test 6
7 experimental B pre-test 7
8 experimental B post-test 8


My goal is to draw a graph like this:



enter image description here



I tried:



ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
geom_line() +
geom_point()


But it returns this error:



Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line


What am I missing?










share|improve this question





























    8














    I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.



    As an exemple, my data looks like this:



    DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
    measure = rep(c("A", "A", "B", "B"), 2),
    time = rep(c("pre-test", "post-test"), 4),
    score = 1:8)

    > DT
    condition measure time score
    1 control A pre-test 1
    2 control A post-test 2
    3 control B pre-test 3
    4 control B post-test 4
    5 experimental A pre-test 5
    6 experimental A post-test 6
    7 experimental B pre-test 7
    8 experimental B post-test 8


    My goal is to draw a graph like this:



    enter image description here



    I tried:



    ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
    geom_line() +
    geom_point()


    But it returns this error:



    Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line


    What am I missing?










    share|improve this question



























      8












      8








      8


      3





      I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.



      As an exemple, my data looks like this:



      DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
      measure = rep(c("A", "A", "B", "B"), 2),
      time = rep(c("pre-test", "post-test"), 4),
      score = 1:8)

      > DT
      condition measure time score
      1 control A pre-test 1
      2 control A post-test 2
      3 control B pre-test 3
      4 control B post-test 4
      5 experimental A pre-test 5
      6 experimental A post-test 6
      7 experimental B pre-test 7
      8 experimental B post-test 8


      My goal is to draw a graph like this:



      enter image description here



      I tried:



      ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
      geom_line() +
      geom_point()


      But it returns this error:



      Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line


      What am I missing?










      share|improve this question















      I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.



      As an exemple, my data looks like this:



      DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
      measure = rep(c("A", "A", "B", "B"), 2),
      time = rep(c("pre-test", "post-test"), 4),
      score = 1:8)

      > DT
      condition measure time score
      1 control A pre-test 1
      2 control A post-test 2
      3 control B pre-test 3
      4 control B post-test 4
      5 experimental A pre-test 5
      6 experimental A post-test 6
      7 experimental B pre-test 7
      8 experimental B post-test 8


      My goal is to draw a graph like this:



      enter image description here



      I tried:



      ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
      geom_line() +
      geom_point()


      But it returns this error:



      Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line


      What am I missing?







      r ggplot2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 2 '18 at 16:08

























      asked Dec 2 '18 at 15:36









      mat

      514730




      514730
























          2 Answers
          2






          active

          oldest

          votes


















          7














          You want to use



          ggplot(DT, aes(time, score, group = interaction(measure, condition), 
          color = measure, linetype = condition)) +
          geom_line() + geom_point()


          enter image description here



          because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.






          share|improve this answer





























            4














            data.frame(
            condition = rep(c("control", "experimental"), each = 4),
            measure = rep(c("A", "A", "B", "B"), 2),
            time = rep(c("pre-test", "post-test"), 4),
            score = 1:8
            ) -> DT

            DT_wide <- tidyr::spread(DT, time, score)

            ggplot() +
            geom_segment(
            data = DT_wide,
            aes(
            x = "pre-test", xend = "post-test",
            y = `pre-test`, yend = `post-test`,
            color = measure,
            linetype = condition
            )
            ) +
            geom_point(
            data = DT,
            aes(time, score, color = measure)
            )


            enter image description here






            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%2f53581787%2fggplot2-combining-group-color-and-linetype%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









              7














              You want to use



              ggplot(DT, aes(time, score, group = interaction(measure, condition), 
              color = measure, linetype = condition)) +
              geom_line() + geom_point()


              enter image description here



              because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.






              share|improve this answer


























                7














                You want to use



                ggplot(DT, aes(time, score, group = interaction(measure, condition), 
                color = measure, linetype = condition)) +
                geom_line() + geom_point()


                enter image description here



                because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.






                share|improve this answer
























                  7












                  7








                  7






                  You want to use



                  ggplot(DT, aes(time, score, group = interaction(measure, condition), 
                  color = measure, linetype = condition)) +
                  geom_line() + geom_point()


                  enter image description here



                  because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.






                  share|improve this answer












                  You want to use



                  ggplot(DT, aes(time, score, group = interaction(measure, condition), 
                  color = measure, linetype = condition)) +
                  geom_line() + geom_point()


                  enter image description here



                  because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 2 '18 at 15:49









                  Julius Vainora

                  32.3k75979




                  32.3k75979

























                      4














                      data.frame(
                      condition = rep(c("control", "experimental"), each = 4),
                      measure = rep(c("A", "A", "B", "B"), 2),
                      time = rep(c("pre-test", "post-test"), 4),
                      score = 1:8
                      ) -> DT

                      DT_wide <- tidyr::spread(DT, time, score)

                      ggplot() +
                      geom_segment(
                      data = DT_wide,
                      aes(
                      x = "pre-test", xend = "post-test",
                      y = `pre-test`, yend = `post-test`,
                      color = measure,
                      linetype = condition
                      )
                      ) +
                      geom_point(
                      data = DT,
                      aes(time, score, color = measure)
                      )


                      enter image description here






                      share|improve this answer


























                        4














                        data.frame(
                        condition = rep(c("control", "experimental"), each = 4),
                        measure = rep(c("A", "A", "B", "B"), 2),
                        time = rep(c("pre-test", "post-test"), 4),
                        score = 1:8
                        ) -> DT

                        DT_wide <- tidyr::spread(DT, time, score)

                        ggplot() +
                        geom_segment(
                        data = DT_wide,
                        aes(
                        x = "pre-test", xend = "post-test",
                        y = `pre-test`, yend = `post-test`,
                        color = measure,
                        linetype = condition
                        )
                        ) +
                        geom_point(
                        data = DT,
                        aes(time, score, color = measure)
                        )


                        enter image description here






                        share|improve this answer
























                          4












                          4








                          4






                          data.frame(
                          condition = rep(c("control", "experimental"), each = 4),
                          measure = rep(c("A", "A", "B", "B"), 2),
                          time = rep(c("pre-test", "post-test"), 4),
                          score = 1:8
                          ) -> DT

                          DT_wide <- tidyr::spread(DT, time, score)

                          ggplot() +
                          geom_segment(
                          data = DT_wide,
                          aes(
                          x = "pre-test", xend = "post-test",
                          y = `pre-test`, yend = `post-test`,
                          color = measure,
                          linetype = condition
                          )
                          ) +
                          geom_point(
                          data = DT,
                          aes(time, score, color = measure)
                          )


                          enter image description here






                          share|improve this answer












                          data.frame(
                          condition = rep(c("control", "experimental"), each = 4),
                          measure = rep(c("A", "A", "B", "B"), 2),
                          time = rep(c("pre-test", "post-test"), 4),
                          score = 1:8
                          ) -> DT

                          DT_wide <- tidyr::spread(DT, time, score)

                          ggplot() +
                          geom_segment(
                          data = DT_wide,
                          aes(
                          x = "pre-test", xend = "post-test",
                          y = `pre-test`, yend = `post-test`,
                          color = measure,
                          linetype = condition
                          )
                          ) +
                          geom_point(
                          data = DT,
                          aes(time, score, color = measure)
                          )


                          enter image description here







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 2 '18 at 15:49









                          hrbrmstr

                          60.1k686148




                          60.1k686148






























                              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%2f53581787%2fggplot2-combining-group-color-and-linetype%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

                              QoS: MAC-Priority for clients behind a repeater

                              Ивакино (Тотемский район)

                              Can't locate Autom4te/ChannelDefs.pm in @INC (when it definitely is there)