Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • change the color of bars -BOXPLOT

    Hi to everybody,
    I have done for the first time "Box plot by group with data point"

    By default, Stata gives me graphs with the same colors. Does anyone know how to change the color of the boxes? I would need 9 different colors (one for Pups_a, one colour for Pups_b ecc)
    below the syntax I wrote


    twoway rbar lqt med Contesto_num, barw(.5) fcolor(gs12) lcolor(black) || ///
    rbar med uqt Contesto_num, barw(.5) fcolor(gs12) lcolor(black) || ///
    scatter Complex Contesto_num, graphregion(fcolor(gs15)) mcolor(black) msymbol(Oh) ///
    legend(off) xlabel( 1 ".Pups_a" 2 ".Pups_b" 3 ".Pups_c" 4 ".aleF_a" 5 ".aleF_b" 6 ".aleF_c" 7 "FF_a" 8 "FF_b" 9 "FF_a" ) ///
    ytitle(Complex score)


    Thanks to everybody

  • #2
    twoway rbar lqt med Contesto_num, barw(.5)
    The command defines one graph. If you want multiple plots, a legend will be necessary, otherwise differentiating bars makes no sense if there is no extra information to be conveyed. The idea is to create multiple plots where below, a loop is helpful.

    Code:
    webuse grunfeld, clear
    keep if time<=9
    twoway rbar invest mvalue time, barw(.5) scheme(s1color) saving(gr1, replace)
    qui separate invest, by(time)
    qui separate mvalue, by(time)
    local all
    forval i=1/9{
        local all "`all'  (rbar invest`i' mvalue`i' time, barw(0.5))"
    }
    twoway `all', scheme(s1color) saving(gr2, replace) leg(off)
    gr combine gr1.gph gr2.gph, scheme(s1olor)
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	20.5 KB
ID:	1587879


    Comment


    • #3
      thank you so much...
      but i have to do "Box plot by group with data point" and not istograms.


      Comment


      • #4
        You need to append the rest of the code if the totality is what creates the box plot. I was just focusing on the point of different colored bars (or boxes, if you wish).

        Comment


        • #5
          Thanks Andrew,
          and Happy new year.

          I tryed to do what you've said to me but i'm failed


          I should get a graph like the picture. where is the mistake in what i wrote?

          (I should use also the same colours)
          I thank you infinitely



          Click image for larger version

Name:	example_boxplot.png
Views:	1
Size:	18.0 KB
ID:	1588636


          Comment


          • #6
            i wrote this but it's not correct.

            twoway rbar lqt med Contesto_num, barw(.5) scheme(s1color) saving(gr1, replace)
            qui separate med, by(Contesto_num)
            qui separate lqt, by(Contesto_num)
            local all
            forval i=1/9{
            local all "`all' (rbar med`i' lqt`i' Contesto_num, barw(0.5))"
            }
            twoway `all', scheme(s1color) saving(gr2, replace) leg(off)
            gr combine gr1.gph gr2.gph, scheme(s1olor)
            scatter Complex Contesto_num
            legend(off) xlabel( 1 ".Pups_a" 2 ".Pups_b" 3 ".Pups_c" 4 ".aleF_a" 5 ".aleF_b" 6 ".aleF_c" 7 "FF_a" 8 "FF_b" 9 "FF_c" ) ///
            ytitle(Complex score)

            Comment


            • #7
              Can you post a sample of your data using dataex?

              Comment


              • #8
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input byte Subject str7 Contesto double Complex
                 1 "FF_a"      .546331467862883
                 2 "FF_a"       .39509966728639
                 3 "FF_a"     .4077292966966801
                 4 "FF_a"     .5569435640666627
                 6 "FF_a"     .5694439971635508
                 7 "FF_a"     .7668754084914023
                 9 "FF_a"      .529592245288984
                10 "FF_a"    .38977499661159526
                 7 "FF_b"    .38158529505582245
                 8 "FF_b"     .5090677539682013
                 9 "FF_b"     .4452743791553812
                10 "FF_b"     .4665977347107954
                11 "FF_b"    .23522146450886783
                12 "FF_b"    .46746811664627574
                13 "FF_b"     .5486848472063541
                 1 "FF_c"     .7978107858441659
                 2 "FF_c"     .7255134367282434
                 3 "FF_c"     .6843173508124704
                 4 "FF_c"     .7379227955206507
                 5 "FF_c"     .5273927071330573
                 7 "FF_c"     .7284914666386457
                 8 "FF_c"    1.2165012365433783
                10 "FF_c"     .5639426413606289
                11 "FF_c"    1.0451232299410538
                12 "FF_c"     .8758913890207222
                13 "FF_c"     .6881810322886441
                14 "FF_c"     .7944895734394164
                 1 "aleF_a"   .4642699836551305
                 3 "aleF_a"   .4229081000494186
                10 "aleF_a"   .7853981633974484
                11 "aleF_a"   .3939347372399995
                12 "aleF_a"    .595165379714826
                14 "aleF_a"   .4876750483210819
                 1 "aleF_b"   .5373842651993277
                 2 "aleF_b"   .7262684537097149
                 7 "aleF_b"   .3639322076101682
                 8 "aleF_b"   .8832436898300681
                10 "aleF_b"   .9697814053066289
                11 "aleF_b"    .614624423286827
                12 "aleF_b"   .6135877788059929
                13 "aleF_b"   .7345082070708817
                 1 "aleF_c"   1.041738426714682
                 8 "aleF_c"   .8942245151603748
                 9 "aleF_c"   .9353315637689095
                10 "aleF_c"   .8835108575164353
                11 "aleF_c"   .7901004270698991
                12 "aleF_c"    .646448444729729
                13 "aleF_c"  1.0697033135295395
                 1 ".Pups_a"  .5050535370773326
                 1 ".Pups_a"   .544451375681954
                 1 ".Pups_a"  .1399046578562445
                 1 ".Pups_a"   .310797463107646
                 1 ".Pups_a"  .2876560949628697
                 1 ".Pups_a" .49772785297418687
                 1 ".Pups_a" .24990671744613882
                 1 ".Pups_a"  .4820012402222069
                 1 ".Pups_b"   .376101832364541
                 1 ".Pups_b"  .5871421345546757
                 1 ".Pups_b"    .76272758825471
                 1 ".Pups_b" .46834850397523203
                 1 ".Pups_b"   .716973743737547
                 1 ".Pups_b"  .6021712195327014
                 1 ".Pups_c"  .5395297632415174
                 1 ".Pups_c"  .4143409943426507
                 1 ".Pups_c"  .6540422639407324
                 1 ".Pups_c"  .5256190648164841
                 1 ".Pups_c"  .5784077328865905
                 1 ".Pups_c"  .3813347725502865
                 1 ".Pups_c"  .5904671674641082
                end
                ------------------ copy up to and including the previous line ------------------

                Comment


                • #9
                  Godd morning
                  and thanks a lot Andrew

                  Comment


                  • #10
                    There are variables missing in #8 from your code in #1. Do you generate these subsequently? If so, also enclose the code. Otherwise, ensure that the following variables are present.

                    Code:
                    dataex Subject lqt med uqt Complex Contesto_num

                    Comment


                    • #11
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input byte Subject str7 Contesto double Complex float(med lqt uqt Contesto_num)
                       1 ".Pups_a"  .4820012402222069 .39639935 .2687814  .5013907 1
                       1 ".Pups_a"  .2876560949628697 .39639935 .2687814  .5013907 1
                       1 ".Pups_a"  .1399046578562445 .39639935 .2687814  .5013907 1
                       1 ".Pups_a"   .544451375681954 .39639935 .2687814  .5013907 1
                       1 ".Pups_a"  .5050535370773326 .39639935 .2687814  .5013907 1
                       1 ".Pups_a" .49772785297418687 .39639935 .2687814  .5013907 1
                       1 ".Pups_a"   .310797463107646 .39639935 .2687814  .5013907 1
                       1 ".Pups_a" .24990671744613882 .39639935 .2687814  .5013907 1
                       1 ".Pups_b"   .376101832364541  .5946567 .4683485  .7169737 2
                       1 ".Pups_b"  .5871421345546757  .5946567 .4683485  .7169737 2
                       1 ".Pups_b"    .76272758825471  .5946567 .4683485  .7169737 2
                       1 ".Pups_b"   .716973743737547  .5946567 .4683485  .7169737 2
                       1 ".Pups_b" .46834850397523203  .5946567 .4683485  .7169737 2
                       1 ".Pups_b"  .6021712195327014  .5946567 .4683485  .7169737 2
                       1 ".Pups_c"  .5395297632415174 .53952974  .414341 .59046715 3
                       1 ".Pups_c"  .3813347725502865 .53952974  .414341 .59046715 3
                       1 ".Pups_c"  .5784077328865905 .53952974  .414341 .59046715 3
                       1 ".Pups_c"  .5256190648164841 .53952974  .414341 .59046715 3
                       1 ".Pups_c"  .4143409943426507 .53952974  .414341 .59046715 3
                       1 ".Pups_c"  .5904671674641082 .53952974  .414341 .59046715 3
                       1 ".Pups_c"  .6540422639407324 .53952974  .414341 .59046715 3
                       2 "FF_a"       .39509966728639 .53796184 .4014145  .5631938 7
                       1 "FF_a"      .546331467862883 .53796184 .4014145  .5631938 7
                      10 "FF_a"    .38977499661159526 .53796184 .4014145  .5631938 7
                       3 "FF_a"     .4077292966966801 .53796184 .4014145  .5631938 7
                       7 "FF_a"     .7668754084914023 .53796184 .4014145  .5631938 7
                       9 "FF_a"      .529592245288984 .53796184 .4014145  .5631938 7
                       6 "FF_a"     .5694439971635508 .53796184 .4014145  .5631938 7
                       4 "FF_a"     .5569435640666627 .53796184 .4014145  .5631938 7
                      11 "FF_b"    .23522146450886783  .4665977 .3815853  .5090678 8
                       9 "FF_b"     .4452743791553812  .4665977 .3815853  .5090678 8
                       8 "FF_b"     .5090677539682013  .4665977 .3815853  .5090678 8
                      10 "FF_b"     .4665977347107954  .4665977 .3815853  .5090678 8
                      12 "FF_b"    .46746811664627574  .4665977 .3815853  .5090678 8
                       7 "FF_b"    .38158529505582245  .4665977 .3815853  .5090678 8
                      13 "FF_b"     .5486848472063541  .4665977 .3815853  .5090678 8
                      11 "FF_c"    1.0451232299410538  .7332071 .6862492  .8368511 9
                      12 "FF_c"     .8758913890207222  .7332071 .6862492  .8368511 9
                      13 "FF_c"     .6881810322886441  .7332071 .6862492  .8368511 9
                       5 "FF_c"     .5273927071330573  .7332071 .6862492  .8368511 9
                      14 "FF_c"     .7944895734394164  .7332071 .6862492  .8368511 9
                       7 "FF_c"     .7284914666386457  .7332071 .6862492  .8368511 9
                       1 "FF_c"     .7978107858441659  .7332071 .6862492  .8368511 9
                       8 "FF_c"    1.2165012365433783  .7332071 .6862492  .8368511 9
                      10 "FF_c"     .5639426413606289  .7332071 .6862492  .8368511 9
                       4 "FF_c"     .7379227955206507  .7332071 .6862492  .8368511 9
                       2 "FF_c"     .7255134367282434  .7332071 .6862492  .8368511 9
                       3 "FF_c"     .6843173508124704  .7332071 .6862492  .8368511 9
                      14 "aleF_a"   .4876750483210819  .4759725 .4229081  .5951654 4
                       3 "aleF_a"   .4229081000494186  .4759725 .4229081  .5951654 4
                      10 "aleF_a"   .7853981633974484  .4759725 .4229081  .5951654 4
                      12 "aleF_a"    .595165379714826  .4759725 .4229081  .5951654 4
                       1 "aleF_a"   .4642699836551305  .4759725 .4229081  .5951654 4
                      11 "aleF_a"   .3939347372399995  .4759725 .4229081  .5951654 4
                       2 "aleF_b"   .7262684537097149  .6704465  .575486   .808876 5
                       7 "aleF_b"   .3639322076101682  .6704465  .575486   .808876 5
                      11 "aleF_b"    .614624423286827  .6704465  .575486   .808876 5
                      13 "aleF_b"   .7345082070708817  .6704465  .575486   .808876 5
                       1 "aleF_b"   .5373842651993277  .6704465  .575486   .808876 5
                      10 "aleF_b"   .9697814053066289  .6704465  .575486   .808876 5
                      12 "aleF_b"   .6135877788059929  .6704465  .575486   .808876 5
                       8 "aleF_b"   .8832436898300681  .6704465  .575486   .808876 5
                      10 "aleF_c"   .8835108575164353  .8942245 .7901005 1.0417384 6
                      11 "aleF_c"   .7901004270698991  .8942245 .7901005 1.0417384 6
                       9 "aleF_c"   .9353315637689095  .8942245 .7901005 1.0417384 6
                       8 "aleF_c"   .8942245151603748  .8942245 .7901005 1.0417384 6
                       1 "aleF_c"   1.041738426714682  .8942245 .7901005 1.0417384 6
                      12 "aleF_c"    .646448444729729  .8942245 .7901005 1.0417384 6
                      13 "aleF_c"  1.0697033135295395  .8942245 .7901005 1.0417384 6
                      end
                      ------------------ copy up to and including the previous line ------------------

                      Comment


                      • #12
                        thanks a lot

                        Comment


                        • #13
                          is it correct the dataset?
                          thanks a lot

                          Comment


                          • #14
                            Thanks for the data example. Note that you can edit a post within one hour of first posting.

                            This may help. Note that many cosmetic details are at choice.


                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input byte Subject str7 Contesto double Complex float(med lqt uqt Contesto_num)
                             1 ".Pups_a"  .4820012402222069 .39639935 .2687814  .5013907 1
                             1 ".Pups_a"  .2876560949628697 .39639935 .2687814  .5013907 1
                             1 ".Pups_a"  .1399046578562445 .39639935 .2687814  .5013907 1
                             1 ".Pups_a"   .544451375681954 .39639935 .2687814  .5013907 1
                             1 ".Pups_a"  .5050535370773326 .39639935 .2687814  .5013907 1
                             1 ".Pups_a" .49772785297418687 .39639935 .2687814  .5013907 1
                             1 ".Pups_a"   .310797463107646 .39639935 .2687814  .5013907 1
                             1 ".Pups_a" .24990671744613882 .39639935 .2687814  .5013907 1
                             1 ".Pups_b"   .376101832364541  .5946567 .4683485  .7169737 2
                             1 ".Pups_b"  .5871421345546757  .5946567 .4683485  .7169737 2
                             1 ".Pups_b"    .76272758825471  .5946567 .4683485  .7169737 2
                             1 ".Pups_b"   .716973743737547  .5946567 .4683485  .7169737 2
                             1 ".Pups_b" .46834850397523203  .5946567 .4683485  .7169737 2
                             1 ".Pups_b"  .6021712195327014  .5946567 .4683485  .7169737 2
                             1 ".Pups_c"  .5395297632415174 .53952974  .414341 .59046715 3
                             1 ".Pups_c"  .3813347725502865 .53952974  .414341 .59046715 3
                             1 ".Pups_c"  .5784077328865905 .53952974  .414341 .59046715 3
                             1 ".Pups_c"  .5256190648164841 .53952974  .414341 .59046715 3
                             1 ".Pups_c"  .4143409943426507 .53952974  .414341 .59046715 3
                             1 ".Pups_c"  .5904671674641082 .53952974  .414341 .59046715 3
                             1 ".Pups_c"  .6540422639407324 .53952974  .414341 .59046715 3
                             2 "FF_a"       .39509966728639 .53796184 .4014145  .5631938 7
                             1 "FF_a"      .546331467862883 .53796184 .4014145  .5631938 7
                            10 "FF_a"    .38977499661159526 .53796184 .4014145  .5631938 7
                             3 "FF_a"     .4077292966966801 .53796184 .4014145  .5631938 7
                             7 "FF_a"     .7668754084914023 .53796184 .4014145  .5631938 7
                             9 "FF_a"      .529592245288984 .53796184 .4014145  .5631938 7
                             6 "FF_a"     .5694439971635508 .53796184 .4014145  .5631938 7
                             4 "FF_a"     .5569435640666627 .53796184 .4014145  .5631938 7
                            11 "FF_b"    .23522146450886783  .4665977 .3815853  .5090678 8
                             9 "FF_b"     .4452743791553812  .4665977 .3815853  .5090678 8
                             8 "FF_b"     .5090677539682013  .4665977 .3815853  .5090678 8
                            10 "FF_b"     .4665977347107954  .4665977 .3815853  .5090678 8
                            12 "FF_b"    .46746811664627574  .4665977 .3815853  .5090678 8
                             7 "FF_b"    .38158529505582245  .4665977 .3815853  .5090678 8
                            13 "FF_b"     .5486848472063541  .4665977 .3815853  .5090678 8
                            11 "FF_c"    1.0451232299410538  .7332071 .6862492  .8368511 9
                            12 "FF_c"     .8758913890207222  .7332071 .6862492  .8368511 9
                            13 "FF_c"     .6881810322886441  .7332071 .6862492  .8368511 9
                             5 "FF_c"     .5273927071330573  .7332071 .6862492  .8368511 9
                            14 "FF_c"     .7944895734394164  .7332071 .6862492  .8368511 9
                             7 "FF_c"     .7284914666386457  .7332071 .6862492  .8368511 9
                             1 "FF_c"     .7978107858441659  .7332071 .6862492  .8368511 9
                             8 "FF_c"    1.2165012365433783  .7332071 .6862492  .8368511 9
                            10 "FF_c"     .5639426413606289  .7332071 .6862492  .8368511 9
                             4 "FF_c"     .7379227955206507  .7332071 .6862492  .8368511 9
                             2 "FF_c"     .7255134367282434  .7332071 .6862492  .8368511 9
                             3 "FF_c"     .6843173508124704  .7332071 .6862492  .8368511 9
                            14 "aleF_a"   .4876750483210819  .4759725 .4229081  .5951654 4
                             3 "aleF_a"   .4229081000494186  .4759725 .4229081  .5951654 4
                            10 "aleF_a"   .7853981633974484  .4759725 .4229081  .5951654 4
                            12 "aleF_a"    .595165379714826  .4759725 .4229081  .5951654 4
                             1 "aleF_a"   .4642699836551305  .4759725 .4229081  .5951654 4
                            11 "aleF_a"   .3939347372399995  .4759725 .4229081  .5951654 4
                             2 "aleF_b"   .7262684537097149  .6704465  .575486   .808876 5
                             7 "aleF_b"   .3639322076101682  .6704465  .575486   .808876 5
                            11 "aleF_b"    .614624423286827  .6704465  .575486   .808876 5
                            13 "aleF_b"   .7345082070708817  .6704465  .575486   .808876 5
                             1 "aleF_b"   .5373842651993277  .6704465  .575486   .808876 5
                            10 "aleF_b"   .9697814053066289  .6704465  .575486   .808876 5
                            12 "aleF_b"   .6135877788059929  .6704465  .575486   .808876 5
                             8 "aleF_b"   .8832436898300681  .6704465  .575486   .808876 5
                            10 "aleF_c"   .8835108575164353  .8942245 .7901005 1.0417384 6
                            11 "aleF_c"   .7901004270698991  .8942245 .7901005 1.0417384 6
                             9 "aleF_c"   .9353315637689095  .8942245 .7901005 1.0417384 6
                             8 "aleF_c"   .8942245151603748  .8942245 .7901005 1.0417384 6
                             1 "aleF_c"   1.041738426714682  .8942245 .7901005 1.0417384 6
                            12 "aleF_c"    .646448444729729  .8942245 .7901005 1.0417384 6
                            13 "aleF_c"  1.0697033135295395  .8942245 .7901005 1.0417384 6
                            end
                            
                            label def Contesto 1 ".Pups_a" 2 ".Pups_b" 3 ".Pups_c" 5 "FF_a"  6 "FF_b" 7 "FF_c" 9 "aleF_a" 10 "aleF_b" 11 "aleF_c"
                            encode Contesto, gen(axis) label(Contesto)
                            
                            local colours orange*0.1 orange*0.3 orange*0.5 blue*0.1 blue*0.3 blue*0.5 green*0.1 green*0.3 green*0.5
                            
                            local Colours orange orange orange blue blue blue green green green
                            
                            local values 1 2 3 5 6 7 9 10 11
                            
                            set scheme s1color
                            
                            forval j = 1/9 {
                                local c : word `j' of `colours'
                                local C : word `j' of `Colours'
                                local v : word `j' of `values'
                                
                                local call `call' || rbar lqt med axis if axis == `v', barw(0.6) fcolor(`c') lcolor(`C') ///
                                || rbar uqt med axis if axis == `v', barw(0.6) fcolor(`c') lcolor(`C')                   ///
                                || scatter Complex axis if axis == `v', mc(`C') ms(Oh)
                            }
                            
                            twoway `call' xla(1 2 3 5 6 7 9 10 11, noticks valuelabel labsize(small)) legend(off) ytitle(Complex) xtitle("") yla(, ang(h)) xli(4 8, lw(vthin) lc(gs12))
                            Click image for larger version

Name:	multicolourbox.png
Views:	1
Size:	44.4 KB
ID:	1588846

                            Comment


                            • #15
                              thank you...
                              i use stata 16
                              thank you a lot for the code

                              Comment

                              Working...
                              X