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

        Working...
        X