Announcement

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

  • bar graph over two variables: unable to adjust the color of bars

    Hi experts;

    I am plotting a bar graph that has two variables. My code looks like below:

    #delimit ;
    graph bar y_var, over(x_var1, relabel(1 "1" 2 "2" 3 "3" 4 "4") label(labsize(vsmall) alternate) gap (5)) over(x_var2, gap(100))

    bar(1, col(gs4))
    bar(2, col(gs8))
    bar(3, col(gs12))
    bar(4, col(gs16))

    {other options}

    ;
    #delimit cr

    The issue is that the "bar" commands don't work when you have two variables over which you are sketching your plot. Any experience how I can fix the colours?

    Thank you


  • #2
    Typically the option asyvars is needed to allow different colours.

    Comment


    • #3
      The example below (not a beautiful one) shows the use of - asyvars - where Stata selects the colors, plus the uso of customized colors, plus the use of grey style renditions:

      Code:
      sysuse auto
      gen highmpg = mpg > 22
      graph bar (count) foreign, over(rep78) over(highmpg) asyvars
      graph bar (count) foreign, over(rep78) over(highmpg) asyvars bar(1, fcolor(yellow)) bar(2, fcolor(red)) bar(3, fcolor(orange)) bar(4, fcolor(green)) bar(5, fcolor(blue))
      graph bar (count) foreign, over(rep78) over(highmpg) asyvars bar(1, fcolor(gs2)) bar(2, fcolor(gs4)) bar(3, fcolor(gs6)) bar(4, fcolor(gs8)) bar(5, fcolor(gs10))
      Best regards,

      Marcos

      Comment


      • #4
        Hi

        Thank you for your tips. Surprisingly the sysuse worked but my code still struggles even after adding the asyvars. I also added (count) to the code like what you shared from the sysuse.
        Here is the error I am getting:
        "
        option bar() not allowed
        r(198);
        "

        And this is my full graph code (variables with new fake names):

        #delimit ;
        graph bar yvar, over(xvar1) over(xvar2) asyvars
        bar(1, fcolor(red))
        bar(2, fcolor(blue))
        bar(3, fcolor(green))
        bar(4, fcolor(black))

        ytitle("{Y-var description}", size(large))
        yscale(range(0 30) )
        ylabel(0 (5) 30, nogrid angle(horizontal) valuelabel tposition(crossing) labsize(medlarge))

        legend(off)
        plotregion(fcolor(white) lcolor(black) lwidth(medium) margin(small) lpattern(solid) ifcolor(none) ilcolor(none) ilwidth(none) ilpattern(solid))
        graphregion(color(white) margin(l=3 r=3 b=13 t=3))
        scheme(s1mono)

        ;
        #delimit cr

        And it doesn't work.

        Also, another question: Can we do rcaps (adding error bars) on the bars of a graph (that would be my next step). I guess if the answer is no, the best bet is to two-way bar graph instead of a bar graph.

        Thank you

        Comment


        • #5
          If the example we showed ‘worked’ well, then there must be something related to the data set or the ‘real’ command you used.

          The FAQ advice underlines how to share a fraction of the data as well as command.
          Best regards,

          Marcos

          Comment

          Working...
          X