Announcement

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

  • Changing bar colors in bar graph

    Hi,
    I am trying to generate a bar graph in which the 15th bar is black and all the other bars grey. Unfortunately, all the bars are grey with the code below.

    Code:
    graph hbar (mean) yvar if xvar != 15 & xvar != 16 [aw=phrf], title("TITLE") ///
    over(xvar, gap(50) relabel(1 "label1" 2 "label2" 3 "label3" 4 "label4" 5 "label5" 6 "label6" 7 "label7" 8 "label8" 9 "label9" 10 "label10" 11 "label11" 12 "label12" 13 "label13" 14 "label14" 15 "label15") label(labsize(vsmall)) sort(1) descending) ///
    ytitle(% agreement, margin(medium) size(small)) legend(off) ///
    ylabel(0 0.2 "20" 0.4 "40" 0.6 "60" 0.8 "80" 1.0 "100", labsize(vsmall)) ///
    bar(1, color(gs7)) bar(2, color(gs7)) bar(3, color(gs7)) ///
    bar(4, color(gs7)) bar(5, color(gs7)) bar(6, color(gs7)) ///
    bar(7, color(gs7)) bar(8, color(gs7)) bar(9, color(gs7)) ///
    bar(10, color(gs7)) bar(11, color(gs7)) bar(12, color(gs7)) ///
    bar(13, color(gs7)) bar(14, color(gs7)) bar(15, color(black)) ///    
    graphregion(color(white)) ///
        note("...", ///
                    size(vsmall) span margin(0 0 0 8))
    Click image for larger version

Name:	bar graph.PNG
Views:	1
Size:	46.1 KB
ID:	1772959


    I know that I could achieve what I want with asyvars. Unfortunately, asyvars makes all the labels into a legend, which does not work for me.
    Do you know how to either change the color of a single bar without asyvars, or use asyvars keeping the labels instead of a legend?
    Thank you!

  • #2
    This need was discussed in

    Code:
    SJ-11-3 gr0049  . . . . . . . . . .  Stata tip 102: Highlighting specific bars
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            Q3/11   SJ 11(3):474--477
            tip on highlighting a subset of observations in a bar or
            dot chart
    which (together with other hits) would have been found in Stata by

    Code:
    search bar, sj 
    I need data to show you in detail, so here is a mock-up of something similar:

    Code:
    clear 
    set obs 15 
    set seed 314159
    gen whatever = runiform(74, 92)
    label var whatever "% of agreement"
    
    gen label = "label" + strofreal(_n)
    
    graph hbar (asis) whatever, over(label, sort(1) descending) ysc(alt) name(G1, replace)
    
    separate whatever, by(label == "label15")
    
    graph hbar (asis) whatever?, nofill over(label, sort(whatever) descending) /// 
    legend(off) ytitle("`: var label whatever'") ysc(alt) name(G2, replace)

    Only the second graph is needed here.

    Click image for larger version

Name:	twocolours.png
Views:	1
Size:	42.1 KB
ID:	1772966


    Notes:

    1. Leave out the (asis) if you do need the default case of means.

    2. Change colours to suit.

    3. Putting the y axis stuff at the top is just a matter of taste, but for more discussion see https://journals.sagepub.com/doi/pdf...867X1201200314

    4. If #1 are real data, then in my view you would be much better off with a dot chart. I suspect the real research issue is comparing values with each other, not with zero.

    Comment


    • #3
      Thank you very much for the suggestion and the further reading!

      Comment

      Working...
      X