How to create a error bar for each color class in R?










0















Here is my code:



ggline(ToothGrowth, x = "dose", y = "len", 
add = c("mean_se", "jitter"),
color = "supp", palette = "jco")


Just for reference, ToothGrowth is at the bottom of this question.



My code keeps giving me this plot:
+/- error bars as a whole
which is the +/- error of all the points, instead of the error bar for each "supp" that I delineated by color. I need it to print something like this: +/- error bars by color group
What am I doing wrong here?



Thank you for the help.



Here is ToothGrowth:



len supp dose
1 4.2 VC 0.5
2 11.5 VC 0.5
3 7.3 VC 0.5
4 5.8 VC 0.5
5 6.4 VC 0.5
6 10.0 VC 0.5
7 11.2 VC 0.5
8 11.2 VC 0.5
9 5.2 VC 0.5
10 7.0 VC 0.5
11 16.5 VC 1
12 16.5 VC 1
13 15.2 VC 1
14 17.3 VC 1
15 22.5 VC 1
16 17.3 VC 1
17 13.6 VC 1
18 14.5 VC 1
19 18.8 VC 1
20 15.5 VC 1
21 23.6 VC 2
22 18.5 VC 2
23 33.9 VC 2
24 25.5 VC 2
25 26.4 VC 2
26 32.5 VC 2
27 26.7 VC 2
28 21.5 VC 2
29 23.3 VC 2
30 29.5 VC 2
31 15.2 OJ 0.5
32 21.5 OJ 0.5
33 17.6 OJ 0.5
34 9.7 OJ 0.5
35 14.5 OJ 0.5
36 10.0 OJ 0.5
37 8.2 OJ 0.5
38 9.4 OJ 0.5
39 16.5 OJ 0.5
40 9.7 OJ 0.5
41 19.7 OJ 1
42 23.3 OJ 1
43 23.6 OJ 1
44 26.4 OJ 1
45 20.0 OJ 1
46 25.2 OJ 1
47 25.8 OJ 1
48 21.2 OJ 1
49 14.5 OJ 1
50 27.3 OJ 1
51 25.5 OJ 2
52 26.4 OJ 2
53 22.4 OJ 2
54 24.5 OJ 2
55 24.8 OJ 2
56 30.9 OJ 2
57 26.4 OJ 2
58 27.3 OJ 2
59 29.4 OJ 2
60 23.0 OJ 2









share|improve this question




























    0















    Here is my code:



    ggline(ToothGrowth, x = "dose", y = "len", 
    add = c("mean_se", "jitter"),
    color = "supp", palette = "jco")


    Just for reference, ToothGrowth is at the bottom of this question.



    My code keeps giving me this plot:
    +/- error bars as a whole
    which is the +/- error of all the points, instead of the error bar for each "supp" that I delineated by color. I need it to print something like this: +/- error bars by color group
    What am I doing wrong here?



    Thank you for the help.



    Here is ToothGrowth:



    len supp dose
    1 4.2 VC 0.5
    2 11.5 VC 0.5
    3 7.3 VC 0.5
    4 5.8 VC 0.5
    5 6.4 VC 0.5
    6 10.0 VC 0.5
    7 11.2 VC 0.5
    8 11.2 VC 0.5
    9 5.2 VC 0.5
    10 7.0 VC 0.5
    11 16.5 VC 1
    12 16.5 VC 1
    13 15.2 VC 1
    14 17.3 VC 1
    15 22.5 VC 1
    16 17.3 VC 1
    17 13.6 VC 1
    18 14.5 VC 1
    19 18.8 VC 1
    20 15.5 VC 1
    21 23.6 VC 2
    22 18.5 VC 2
    23 33.9 VC 2
    24 25.5 VC 2
    25 26.4 VC 2
    26 32.5 VC 2
    27 26.7 VC 2
    28 21.5 VC 2
    29 23.3 VC 2
    30 29.5 VC 2
    31 15.2 OJ 0.5
    32 21.5 OJ 0.5
    33 17.6 OJ 0.5
    34 9.7 OJ 0.5
    35 14.5 OJ 0.5
    36 10.0 OJ 0.5
    37 8.2 OJ 0.5
    38 9.4 OJ 0.5
    39 16.5 OJ 0.5
    40 9.7 OJ 0.5
    41 19.7 OJ 1
    42 23.3 OJ 1
    43 23.6 OJ 1
    44 26.4 OJ 1
    45 20.0 OJ 1
    46 25.2 OJ 1
    47 25.8 OJ 1
    48 21.2 OJ 1
    49 14.5 OJ 1
    50 27.3 OJ 1
    51 25.5 OJ 2
    52 26.4 OJ 2
    53 22.4 OJ 2
    54 24.5 OJ 2
    55 24.8 OJ 2
    56 30.9 OJ 2
    57 26.4 OJ 2
    58 27.3 OJ 2
    59 29.4 OJ 2
    60 23.0 OJ 2









    share|improve this question


























      0












      0








      0








      Here is my code:



      ggline(ToothGrowth, x = "dose", y = "len", 
      add = c("mean_se", "jitter"),
      color = "supp", palette = "jco")


      Just for reference, ToothGrowth is at the bottom of this question.



      My code keeps giving me this plot:
      +/- error bars as a whole
      which is the +/- error of all the points, instead of the error bar for each "supp" that I delineated by color. I need it to print something like this: +/- error bars by color group
      What am I doing wrong here?



      Thank you for the help.



      Here is ToothGrowth:



      len supp dose
      1 4.2 VC 0.5
      2 11.5 VC 0.5
      3 7.3 VC 0.5
      4 5.8 VC 0.5
      5 6.4 VC 0.5
      6 10.0 VC 0.5
      7 11.2 VC 0.5
      8 11.2 VC 0.5
      9 5.2 VC 0.5
      10 7.0 VC 0.5
      11 16.5 VC 1
      12 16.5 VC 1
      13 15.2 VC 1
      14 17.3 VC 1
      15 22.5 VC 1
      16 17.3 VC 1
      17 13.6 VC 1
      18 14.5 VC 1
      19 18.8 VC 1
      20 15.5 VC 1
      21 23.6 VC 2
      22 18.5 VC 2
      23 33.9 VC 2
      24 25.5 VC 2
      25 26.4 VC 2
      26 32.5 VC 2
      27 26.7 VC 2
      28 21.5 VC 2
      29 23.3 VC 2
      30 29.5 VC 2
      31 15.2 OJ 0.5
      32 21.5 OJ 0.5
      33 17.6 OJ 0.5
      34 9.7 OJ 0.5
      35 14.5 OJ 0.5
      36 10.0 OJ 0.5
      37 8.2 OJ 0.5
      38 9.4 OJ 0.5
      39 16.5 OJ 0.5
      40 9.7 OJ 0.5
      41 19.7 OJ 1
      42 23.3 OJ 1
      43 23.6 OJ 1
      44 26.4 OJ 1
      45 20.0 OJ 1
      46 25.2 OJ 1
      47 25.8 OJ 1
      48 21.2 OJ 1
      49 14.5 OJ 1
      50 27.3 OJ 1
      51 25.5 OJ 2
      52 26.4 OJ 2
      53 22.4 OJ 2
      54 24.5 OJ 2
      55 24.8 OJ 2
      56 30.9 OJ 2
      57 26.4 OJ 2
      58 27.3 OJ 2
      59 29.4 OJ 2
      60 23.0 OJ 2









      share|improve this question
















      Here is my code:



      ggline(ToothGrowth, x = "dose", y = "len", 
      add = c("mean_se", "jitter"),
      color = "supp", palette = "jco")


      Just for reference, ToothGrowth is at the bottom of this question.



      My code keeps giving me this plot:
      +/- error bars as a whole
      which is the +/- error of all the points, instead of the error bar for each "supp" that I delineated by color. I need it to print something like this: +/- error bars by color group
      What am I doing wrong here?



      Thank you for the help.



      Here is ToothGrowth:



      len supp dose
      1 4.2 VC 0.5
      2 11.5 VC 0.5
      3 7.3 VC 0.5
      4 5.8 VC 0.5
      5 6.4 VC 0.5
      6 10.0 VC 0.5
      7 11.2 VC 0.5
      8 11.2 VC 0.5
      9 5.2 VC 0.5
      10 7.0 VC 0.5
      11 16.5 VC 1
      12 16.5 VC 1
      13 15.2 VC 1
      14 17.3 VC 1
      15 22.5 VC 1
      16 17.3 VC 1
      17 13.6 VC 1
      18 14.5 VC 1
      19 18.8 VC 1
      20 15.5 VC 1
      21 23.6 VC 2
      22 18.5 VC 2
      23 33.9 VC 2
      24 25.5 VC 2
      25 26.4 VC 2
      26 32.5 VC 2
      27 26.7 VC 2
      28 21.5 VC 2
      29 23.3 VC 2
      30 29.5 VC 2
      31 15.2 OJ 0.5
      32 21.5 OJ 0.5
      33 17.6 OJ 0.5
      34 9.7 OJ 0.5
      35 14.5 OJ 0.5
      36 10.0 OJ 0.5
      37 8.2 OJ 0.5
      38 9.4 OJ 0.5
      39 16.5 OJ 0.5
      40 9.7 OJ 0.5
      41 19.7 OJ 1
      42 23.3 OJ 1
      43 23.6 OJ 1
      44 26.4 OJ 1
      45 20.0 OJ 1
      46 25.2 OJ 1
      47 25.8 OJ 1
      48 21.2 OJ 1
      49 14.5 OJ 1
      50 27.3 OJ 1
      51 25.5 OJ 2
      52 26.4 OJ 2
      53 22.4 OJ 2
      54 24.5 OJ 2
      55 24.8 OJ 2
      56 30.9 OJ 2
      57 26.4 OJ 2
      58 27.3 OJ 2
      59 29.4 OJ 2
      60 23.0 OJ 2






      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 15:24









      s_t

      3,22321031




      3,22321031










      asked Nov 12 '18 at 15:16









      RneryRnery

      1




      1






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I've tried your code, and it seems not having problems, as stated also here.
          However you can also try to use ggplot2 package:



          library(ggsci) # to have the blue and yellow theme
          ggplot(ToothGrowth, aes( x = dose, y = len, color = supp, group = supp )) +
          geom_jitter()+ # jitter it
          stat_summary(fun.y = mean, # here add the summary, I suppose
          fun.ymin = function(x) mean(x) - sd(x), # mean and +-sd
          fun.ymax = function(x) mean(x) + sd(x),
          geom = "errorbar") +
          stat_summary(fun.y= function(x) mean(x), geom="line", aes(group = supp))+
          stat_summary(fun.y = mean,geom = "point") +
          # here you add the line
          scale_color_jco()+ # here the colors
          theme(panel.background = element_rect(fill = "white", colour = "black")) # you can customize
          # many features with ggplot


          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%2f53265079%2fhow-to-create-a-error-bar-for-each-color-class-in-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









            1














            I've tried your code, and it seems not having problems, as stated also here.
            However you can also try to use ggplot2 package:



            library(ggsci) # to have the blue and yellow theme
            ggplot(ToothGrowth, aes( x = dose, y = len, color = supp, group = supp )) +
            geom_jitter()+ # jitter it
            stat_summary(fun.y = mean, # here add the summary, I suppose
            fun.ymin = function(x) mean(x) - sd(x), # mean and +-sd
            fun.ymax = function(x) mean(x) + sd(x),
            geom = "errorbar") +
            stat_summary(fun.y= function(x) mean(x), geom="line", aes(group = supp))+
            stat_summary(fun.y = mean,geom = "point") +
            # here you add the line
            scale_color_jco()+ # here the colors
            theme(panel.background = element_rect(fill = "white", colour = "black")) # you can customize
            # many features with ggplot


            enter image description here






            share|improve this answer





























              1














              I've tried your code, and it seems not having problems, as stated also here.
              However you can also try to use ggplot2 package:



              library(ggsci) # to have the blue and yellow theme
              ggplot(ToothGrowth, aes( x = dose, y = len, color = supp, group = supp )) +
              geom_jitter()+ # jitter it
              stat_summary(fun.y = mean, # here add the summary, I suppose
              fun.ymin = function(x) mean(x) - sd(x), # mean and +-sd
              fun.ymax = function(x) mean(x) + sd(x),
              geom = "errorbar") +
              stat_summary(fun.y= function(x) mean(x), geom="line", aes(group = supp))+
              stat_summary(fun.y = mean,geom = "point") +
              # here you add the line
              scale_color_jco()+ # here the colors
              theme(panel.background = element_rect(fill = "white", colour = "black")) # you can customize
              # many features with ggplot


              enter image description here






              share|improve this answer



























                1












                1








                1







                I've tried your code, and it seems not having problems, as stated also here.
                However you can also try to use ggplot2 package:



                library(ggsci) # to have the blue and yellow theme
                ggplot(ToothGrowth, aes( x = dose, y = len, color = supp, group = supp )) +
                geom_jitter()+ # jitter it
                stat_summary(fun.y = mean, # here add the summary, I suppose
                fun.ymin = function(x) mean(x) - sd(x), # mean and +-sd
                fun.ymax = function(x) mean(x) + sd(x),
                geom = "errorbar") +
                stat_summary(fun.y= function(x) mean(x), geom="line", aes(group = supp))+
                stat_summary(fun.y = mean,geom = "point") +
                # here you add the line
                scale_color_jco()+ # here the colors
                theme(panel.background = element_rect(fill = "white", colour = "black")) # you can customize
                # many features with ggplot


                enter image description here






                share|improve this answer















                I've tried your code, and it seems not having problems, as stated also here.
                However you can also try to use ggplot2 package:



                library(ggsci) # to have the blue and yellow theme
                ggplot(ToothGrowth, aes( x = dose, y = len, color = supp, group = supp )) +
                geom_jitter()+ # jitter it
                stat_summary(fun.y = mean, # here add the summary, I suppose
                fun.ymin = function(x) mean(x) - sd(x), # mean and +-sd
                fun.ymax = function(x) mean(x) + sd(x),
                geom = "errorbar") +
                stat_summary(fun.y= function(x) mean(x), geom="line", aes(group = supp))+
                stat_summary(fun.y = mean,geom = "point") +
                # here you add the line
                scale_color_jco()+ # here the colors
                theme(panel.background = element_rect(fill = "white", colour = "black")) # you can customize
                # many features with ggplot


                enter image description here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 19:17

























                answered Nov 12 '18 at 16:26









                s_ts_t

                3,22321031




                3,22321031



























                    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%2f53265079%2fhow-to-create-a-error-bar-for-each-color-class-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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo