Announcement

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

  • Coloring bars by group- asyvars alternative?

    I am trying to color bars based on the values of a categorical variable

    As described here: https://www.stata.com/statalist/archive/2011-03/msg00098.html

    Code:
    sysuse auto, clear
    gr hbar weight, over(foreign) bar(1, color(black)) bar(2, color(gs8))
    Does not have the desired effect, while

    Code:
    sysuse auto, clear
    gr hbar weight, over(foreign) asyvars bar(1, color(black)) bar(2, color(gs8))
    Does, but also changes the format of the graph.

    Is there an alternative solution that simply allows me to specify the colors?

  • #2
    See https://www.stata-journal.com/sjpdf....iclenum=gr0049 for one discussion. Note that

    Code:
    search bar
    would have found that (together with, necessarily, much else). So, consider

    Code:
    sysuse auto, clear
    
    gr hbar weight, over(foreign) bar(1, color(black)) bar(2, color(gs8)) name(G1, replace)
    
    separate weight, by(foreign) veryshortlabel
    gr hbar weight?, over(foreign) nofill asyvars bar(1, color(black)) bar(2, color(gs8)) name(G2, replace)

    Comment


    • #3
      Thanks Nick. So am I right in thinking that I cannot achieve this without generating a new variable?

      Comment


      • #4
        You can edit each bar individually in the Graph Editor, record it, and then play it back:

        Code:
        sysuse auto, clear
        gr hbar weight, over(foreign) play(bar_color)
        or edit the graph through the keyboard:
        Code:
        sysuse auto, clear
        gr hbar weight, over(foreign) 
        gr_edit plotregion1.bars[1].EditCustomStyle , style(shadestyle(color(lime)))
        gr_edit plotregion1.bars[1].EditCustomStyle , style(linestyle(color(lime)))

        Comment


        • #5
          I didn't know that the editor could be used programmatically- that does the trick. Many thanks Scott!

          Comment

          Working...
          X