Announcement

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

  • Twoway Bar chart with standard errors and multiple colors

    Hi Everyone,

    I have two treatments 1 and 2 and I want to plot mean and se of some variable for both treatments with different colors. I have the following code but that shows same color for both treatments. Can you please how can i modify so that it shows both bars in different colors.
    Code:
    preserve
    statsby mean=r(mean) ub=r(ub) lb=r(lb) N=r(N), by(Treatment) clear: ci mean variable_Y
    twoway bar mean Treatment, barcolor(gs12 navy) barw(0.4) || rcap ub lb Treatment, legend(off) ylabel(140 (10) 200) xlabel(1 "Treatment1" 2 "Treatment2") ytitle(ECU) title(Variable Y)
    restore

  • #2
    No data example here. This is the detonator, dynamite or plunger plot design, much deprecated, especially but not only by people in biostatistics.

    See e.g. https://simplystatistics.org/posts/2...lots-must-die/

    http://users.stat.umn.edu/~rend0020/...4-dynamite.pdf

    Even if you are under instruction to produce one, those colours and other choices don't work well. Here are some small improvements, or so I suggest, but you'd be better off with a dot or strip plot plus confidence intervals.

    I am using Stata 18. See https://www.statalist.org/forums/help#version Crucially, barcolor() doesn't work in this version.

    Code:
    sysuse auto, clear
    
    preserve
    
    gen Treatment = foreign + 1
    
    statsby, by(Treatment) clear: ci mean mpg
    
    foreach v in mean ub lb {
        separate `v', by(Treatment)
    }
    
    twoway bar mean1 Treatment, fcolor(stc1*0.1) barw(0.4) base(0) || ///
           bar mean2 Treatment, fcolor(stc2*0.1) barw(0.4) || ///
           rcap ub1 lb1 Treatment, lw(medium) lc(stc1) || ///
           rcap ub2 lb2 Treatment, lw(medium) lc(stc2) legend(off) yla(0(5)30) ///
           xlabel(1 "Treatment 1" 2 "Treatment 2", noticks) ytitle(whatever) title(better title needed) xtitle("") aspect(1)
    restore
    Click image for larger version

Name:	detonator.png
Views:	1
Size:	31.5 KB
ID:	1730973

    Comment


    • #3
      Thanks a lot for the suggestion. It worked

      Comment


      • #4
        #3 is best understood through a cross-reference to https://www.statalist.org/forums/for...r-to-bar-chart -- where a different plot was recommended by me.

        Comment


        • #5
          #2 Sir, I want to create a bar chart with error bars for my dataset. Following the instructions in note #2, I used the exact command to reproduce the bar chart based on the `sysuse auto, clear` data. However, the last command is not executing properly.
          It's giving the following error message.
          . twoway xlabel(1 "Treatment 1" 2 "Treatment 2", noticks) ytitle(whatever) title(better title needed) xtitle> ("")aspect(1)

          ) required
          r(100);

          Please advise

          Comment


          • #6
            The code you show in #5 is not the code used in #2. You've left out most of the code.

            Comment


            • #7

              Sir, As per note #2, I have followed all the commands listed below, but while executing the last command (in red), I am encountering the following error on STATA. ) required r(100); sysuse auto, clear preserve gen Treatment = foreign + 1 statsby, by(Treatment) clear: ci mean mpg foreach v in mean ub lb { separate `v', by(Treatment) } twoway bar mean1 Treatment, fcolor(stc1*0.1) barw(0.4) base(0) || /// bar mean2 Treatment, fcolor(stc2*0.1) barw(0.4) || /// rcap ub1 lb1 Treatment, lw(medium) lc(stc1) || /// rcap ub2 lb2 Treatment, lw(medium) lc(stc2) legend(off) yla(0(5)30) /// xlabel(1 "Treatment 1" 2 "Treatment 2", noticks) ytitle(whatever) title(better title needed) xtitle("") aspect(1) restore

              Comment


              • #8
                Please use CODE delimiters to show code.

                Again, sorry, but I can't follow what you did that was wrong, unless you introduced an unusual character in copying code. Running a do-file through chartab from SSC might characters like ASCII 160, which looks like a space but could break your code. Copy the code into a string variable to do that.

                Each instance of

                Code:
                ///
                should occur at the end of a code line, as in #2.


                Code:
                . clear
                
                . edit
                
                . *(1 variable, 18 observations pasted into data editor)
                
                . l var1
                
                     +---------------------------------------------------------------------------------------------------+
                     | var1                                                                                              |
                     |---------------------------------------------------------------------------------------------------|
                  1. | sysuse auto, clear                                                                                |
                  2. |                                                                                                   |
                  3. | preserve                                                                                          |
                  4. |                                                                                                   |
                  5. | gen Treatment = foreign + 1                                                                       |
                     |---------------------------------------------------------------------------------------------------|
                  6. |                                                                                                   |
                  7. | statsby, by(Treatment) clear: ci mean mpg                                                         |
                  8. |                                                                                                   |
                  9. | foreach v in mean ub lb {                                                                         |
                 10. |     separate `v', by(Treatment)                                                                   |
                     |---------------------------------------------------------------------------------------------------|
                 11. | }                                                                                                 |
                 12. |                                                                                                   |
                 13. | twoway bar mean1 Treatment, fcolor(stc1*0.1) barw(0.4) base(0) || ///                             |
                 14. |        bar mean2 Treatment, fcolor(stc2*0.1) barw(0.4) || ///                                     |
                 15. |        rcap ub1 lb1 Treatment, lw(medium) lc(stc1) || ///                                         |
                     |---------------------------------------------------------------------------------------------------|
                 16. |        rcap ub2 lb2 Treatment, lw(medium) lc(stc2) legend(off) yla(0(5)30) ///                    |
                 17. |        xlabel(1 "Treatment 1" 2 "Treatment 2", noticks) ytitle(whatever) title(better title nee.. |
                 18. | restore                                                                                           |
                     +---------------------------------------------------------------------------------------------------+
                
                . chartab var1
                
                   decimal  hexadecimal   character |     frequency    unique name
                ------------------------------------+----------------------------------------
                        32       \u0020             |            93    SPACE
                        34       \u0022       "     |             6    QUOTATION MARK
                        39       \u0027       '     |             1    APOSTROPHE
                        40       \u0028       (     |            19    LEFT PARENTHESIS
                        41       \u0029       )     |            19    RIGHT PARENTHESIS
                        42       \u002a       *     |             2    ASTERISK
                        43       \u002b       +     |             1    PLUS SIGN
                        44       \u002c       ,     |             8    COMMA
                        46       \u002e       .     |             4    FULL STOP
                        47       \u002f       /     |            12    SOLIDUS
                        48       \u0030       0     |             7    DIGIT ZERO
                        49       \u0031       1     |            11    DIGIT ONE
                        50       \u0032       2     |             7    DIGIT TWO
                        51       \u0033       3     |             1    DIGIT THREE
                        52       \u0034       4     |             2    DIGIT FOUR
                        53       \u0035       5     |             1    DIGIT FIVE
                        58       \u003a       :     |             1    COLON
                        61       \u003d       =     |             1    EQUALS SIGN
                        84       \u0054       T     |             9    LATIN CAPITAL LETTER T
                        96       \u0060       `     |             1    GRAVE ACCENT
                        97       \u0061       a     |            32    LATIN SMALL LETTER A
                        98       \u0062       b     |            16    LATIN SMALL LETTER B
                        99       \u0063       c     |            16    LATIN SMALL LETTER C
                       100       \u0064       d     |             5    LATIN SMALL LETTER D
                       101       \u0065       e     |            53    LATIN SMALL LETTER E
                       102       \u0066       f     |             6    LATIN SMALL LETTER F
                       103       \u0067       g     |             4    LATIN SMALL LETTER G
                       104       \u0068       h     |             2    LATIN SMALL LETTER H
                       105       \u0069       i     |            10    LATIN SMALL LETTER I
                       107       \u006b       k     |             1    LATIN SMALL LETTER K
                       108       \u006c       l     |            19    LATIN SMALL LETTER L
                       109       \u006d       m     |            18    LATIN SMALL LETTER M
                       110       \u006e       n     |            19    LATIN SMALL LETTER N
                       111       \u006f       o     |            11    LATIN SMALL LETTER O
                       112       \u0070       p     |             6    LATIN SMALL LETTER P
                       114       \u0072       r     |            28    LATIN SMALL LETTER R
                       115       \u0073       s     |            15    LATIN SMALL LETTER S
                       116       \u0074       t     |            41    LATIN SMALL LETTER T
                       117       \u0075       u     |             7    LATIN SMALL LETTER U
                       118       \u0076       v     |             4    LATIN SMALL LETTER V
                       119       \u0077       w     |             7    LATIN SMALL LETTER W
                       120       \u0078       x     |             2    LATIN SMALL LETTER X
                       121       \u0079       y     |             7    LATIN SMALL LETTER Y
                       123       \u007b       {     |             1    LEFT CURLY BRACKET
                       124       \u007c       |     |             6    VERTICAL LINE
                       125       \u007d       }     |             1    RIGHT CURLY BRACKET
                ------------------------------------+----------------------------------------
                
                                                    freq. count   distinct
                ASCII characters              =             543         46
                Multibyte UTF-8 characters    =               0          0
                Unicode replacement character =               0          0
                Total Unicode characters      =             543         46
                
                
                .

                Comment


                • #9
                  Note that

                  Code:
                  restore 
                  is a separate command, and not part of the twoway call.

                  Comment


                  • #10
                    yes sir

                    Comment

                    Working...
                    X