Announcement

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

  • Help with coloring bars when using "over( )" option

    Hello! I'm trying to accomplish something quite simple but can't figure out a way to do it with code. When making a bar graph with the -over( )- option, I'd like to make each set of bars a different color. (I realize that the different panels are already communicating different groups, and that the different colors are therefore needless, but aesthetically there's something nice about using different colors.)

    For example, suppose I run the following:
    Code:
    sysuse auto, clear
    graph bar, over(headroom) by(foreign, total note("") row(1) b1title(Headroom))
    The color of each set of bars is the same. What code would I use to make it so that, for example, the bars in the first panel are blue, bars in second panel are red, and bars in third panel are green? Does Stata have a way to do this?

    Many thanks in advance for your help!

  • #2
    Here is one way. Using expand was already explained in your previous thread.

    https://www.statalist.org/forums/for...rs-for-catvar1

    Code:
    sysuse auto, clear
    graph bar, over(headroom) by(foreign, total note("") row(1) b1title(Headroom)) name(G1, replace)
    
    preserve
    
    expand 2, gen(isnew)
    replace foreign = 2 if isnew
    label def origin 2 "Total", modify
    
    contract headroom foreign, zero
    
    
    separate _freq, by(foreign) veryshortlabel
    
    graph bar _freq?, over(headroom)  nofill by(foreign, b1title(Headroom (inches)) legend(off) row(1) note("")) name(G2, replace)
    
    restore
    Last edited by Nick Cox; 14 Jan 2024, 03:14.

    Comment


    • #3
      Wow, this is terrific! And it looks like I can also add "bar(1, bcolor(red))" "bar(2, bcolor(orange))" etc. options to have complete control over the colors as needed. Thank you so much, Nick!

      Comment


      • #4
        Indeed. But avoid using red and green in the same display. See e.g. https://www.edwardtufte.com/bboard/q...?msg_id=0000HT

        Comment

        Working...
        X