Announcement

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

  • STATA graphing: changing the bar label color for one bar label

    Dear STATA community,

    I am producing figures using the "graph bar" command. Some of my bars are greyscale but some are white. I would like to have the bar label appear in white for the greyscale bars and black for the white bars. Can someone please let me know how to do this? As of now I can only see one way to edit the color of the bar labels (the "blabel" command) and this makes all bar labels one color.

    For reference my code is:

    #delimit ;

    gr bar nationality_clgr_prcnt, over(nationality_cl) over(round)
    asyvars stack blabel(bar, format(%2.0f) size(3) pos(center) color(gs12))
    bargap(0) plotregion(style(none)) graphregion(margin(4 3 3 5 ))
    bar(1, fcolor(gs1) lcolor(gs1) lwidth(.2))
    bar(2, fcolor(gs9) lcolor(gs1) lwidth(.2))
    bar(3, fcolor(gs4) lcolor(gs1) lwidth(.2))
    bar(4, fcolor(gs7) lcolor(gs1) lwidth(.2))
    bar(5, fcolor(white) lcolor(gs1) lwidth(.2))
    ylabel(0 (20) 100,labs(4) nogrid) ytitle("Percentage", size(4)) ysize(13) xsize(20)
    legend( pos(right) rows(5) size(3) order(5 4 3 2 1));



    Thank you!

  • #2
    In the future, please use dataex to present data examples. Here, you cannot specify multiple conditions for blabel() and therefore, the easiest way to handle this is to recreate the graph using twoway. Of course, you could always push the bar labels to outside the bars or use different colors for your bars. However, if you are too keen, the text() option in graph bar provides a way out. The challenge is to figure what the categorical axis values are since there is no x-axis in graph bar. With a sufficiently small graph, you may easily do this but as the graph gets larger, the attractiveness of switching to twoway increases. I borrow Chiara Piazzo's data example from this link to illustrate that using the text() option achieves this goal

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 city byte week float(count z)
    "Boston"  1 3.5  1.9
    "Akron"   1 3.1  1.5
    "NYC"     1 2.8 -4.1
    "Houston" 1 2.5 12.8
    "OKC"     1 2.7  1.7
    "Boston"  2 2.7    2
    "Akron"   2 9.5  1.4
    "NYC"     2 5.3    4
    "Houston" 2 4.5 11.2
    "OKC"     2 2.1    4
    "Boston"  3 2.7  1.7
    "Akron"   3 9.5  1.4
    "NYC"     3 5.3  3.9
    "Houston" 3 4.5 10.9
    "OKC"     3 2.1  4.3
    end
    
    
    // Original graph (bar labels in black font not visible on first bar)
    
    graph hbar count, over(city) asyvars  blabel(bar) blabel(bar, size(3) pos(center) color(black)) /// 
    bargap(0) plotregion(style(none)) bar(1, fcolor(gs1) lcolor(gs1) lwidth(.2)) ///
    bar(2, fcolor(gs9) lcolor(gs1) lwidth(.2)) bar(3, fcolor(gs4) lcolor(gs1) lwidth(.2)) ///
    bar(4, fcolor(gs7) lcolor(gs1) lwidth(.2)) bar(5, fcolor(white) lcolor(gs1) lwidth(.2)) ///
    legend( pos(right) rows(5) size(3) order(5 4 3 2 1)) ytitle("")
    
    gr save gr1
    
    // generate mean count variable
    bys city: egen mcount= mean(count)
    
    // Stata sorts alphabetically by default. generate order
    bys city: gen order= _n==1
    replace order= sum(order) if order==1
    
    //To place labels mid-bar, generate var that is 0.5*mean
    gen ypos= mcount/2
    
    //Now you have the position on the y-axis and the order. Use trial-and error for getting the x-axis position
    list city mcount order ypos if order!=0
    
    //Graph adding text() options and changing bar label color
    
    graph hbar mcount if order!=0, over(city, sort(-order)) asyvars  bargap(0) plotregion(style(none)) ///
    bar(1, fcolor(gs1) lcolor(gs1) lwidth(.2)) bar(2, fcolor(gs9) lcolor(gs1) lwidth(.2)) ///
    bar(3, fcolor(gs4) lcolor(gs1) lwidth(.2)) bar(4, fcolor(gs7) lcolor(gs1) lwidth(.2)) ///
    bar(5, fcolor(white) lcolor(gs1) lwidth(.2)) legend( pos(right) rows(5) size(3) order(5 4 3 2 1)) ///
    text(3.683333 87 "7.36667", color(white) size(small)) text(1.483333 70 "2.96667", size(small)) ///
    text(1.916667 50 "3.83333", color(white) size(small)) text(2.23333 32 "4.46667", size(small)) ///
    text(1.15 13 "2.3", size(small)) ytitle("")
    
    gr save gr2
    
    gr combine gr1.gph gr2.gph
    Code:
    . list city mcount order ypos if order!=0
    
         +---------------------------------------+
         |    city     mcount   order       ypos |
         |---------------------------------------|
      1. |   Akron   7.366667       1   3.683333 |
      4. |  Boston   2.966667       2   1.483333 |
      7. | Houston   3.833333       3   1.916667 |
     10. |     NYC   4.466667       4   2.233333 |
     13. |     OKC        2.3       5       1.15 |
         +---------------------------------------+
    Click image for larger version

Name:	gr_blabel.png
Views:	1
Size:	47.7 KB
ID:	1451514

    Comment

    Working...
    X