Announcement

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

  • Multitple color graph bar

    Dear Stata users,

    I'm trying to produce a twoway graph, plotting size effect as bar and adding confidence interval. Everything run smooth, except when I'm trying to change bar's color to distinguish the different treatment. See code example below:

    Code:
     graph twoway bar T1_C trt, text(.06  .05 "Control" , place(c))                 ///
                                   text(.36 1.05 "Civ. Ed.", place(c))                 ///
                                   text(.22 2.05 "Sec."    , place(c))                 ///
                                   text(.45 3.05 "Combined", place(c))                 ///
                                   bargap(1) barwidth(0.40) legend(off) color(grey%30) lcolor(%30) lcolor(black) ///
                                   title("Civic Participation", box fcolor(white)) xlabel(,nolabels)     ///
                                   xtitle("Treatment") ytitle("Index Civic Participation")            ///
                                   subtitle(" ") graphregion(color(white)) plotregion(color(white)) ///
                                   || ///
                    rcap T1_C1 T1_C2 trt, vertical
    Where, T1_C are the coefficient size, trt an indicator for the different treatment, T1_C1 and T1_C2 lower and upper value of CI interval. I tried to add the line of code below:

    Code:
     bar(1,color(green)) bar(2,color(red))bar(3,color(blue))bar(4,color(orange))
    but it doesn't works. Based on previous post on Statalist it seems that I might being confused between graph bar and bar, but I can't find what's wrong.

    Many thanks in advance,

  • #2
    From the manual:
    The barlook_options are allowed inside graph bar's and graph hbar's option bar(# barlook_options), as in

    . graph bar yvar1 yvar2, bar(1,color(green)) bar(2,color(red))

    The command above would set the bar associated with yvar1 to be green and the bar associated with yvar2 to be red; see [G-2] graph bar.

    barlook_options are also allowed as options with graph twoway bar and graph twoway rbar, as in

    . graph twoway bar yvar xvar, color(green)

    The above would set all the bars (which are located at xvar and extend to yvar) to be green; see [G-2] graph twoway bar and [G-2] graph twoway rbar.
    This means that the following works:
    Code:
    clear all
    sysuse auto
    
    graph bar mpg rep78, name(g1) bar(1, color(green))
    graph twoway bar mpg rep78, name(g2) color(green)
    graph combine g1 g2
    Clearly the charts are very different conceptually, not just by the color. Next, just guessing the intent of Sam Fe


    Code:
    clear all
    sysuse auto
    separate mpg, by(rep78) generate(gmpg)
    graph twoway bar gmpg* rep78, name(g3) color(green red blue orange)
    This results in:
    Click image for larger version

Name:	g3.png
Views:	1
Size:	20.5 KB
ID:	1532040


    Comment


    • #3
      Many thanks Sergyi Radyakin !! You're solution works super fine, I haven't thought about using the separate command but it does the job super great.

      Have a nice evening!

      Comment

      Working...
      X