Announcement

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

  • Is it possible to specify a different color only for the negative bar?

    Hello,

    I want to change the different colors on the negative bar in my graph.

    Code:
    input year    month    growth_rate
    2021    2    -3.463143
    2021    3    -2.50773
    2021    4    1.357074
    2021    5    4.787192
    2021    6    -2.279126
    2021    7    2.595344
    2021    8    2.420688
    2021    9    .4742086
    2021    10    5.618207
    2021    11    -1.425759
    2021    12    -2.704679
    end
    
    graph bar growth_rate, over(month) over(year) stack blabel(total, format(%9.1f))
    I have looked up some answers, but I am having trouble applying them, so I am asking.

    Thank you in advance.

  • #2
    Here's something applied to your data that may start you in a useful direction. I'd probably choose to suppress the legend instead of what I demonstrate.
    Code:
    input year    month    growth_rate
    2021    2    -3.463143
    2021    3    -2.50773
    2021    4    1.357074
    2021    5    4.787192
    2021    6    -2.279126
    2021    7    2.595344
    2021    8    2.420688
    2021    9    .4742086
    2021    10    5.618207
    2021    11    -1.425759
    2021    12    -2.704679
    end
    
    generate gr_pos = cond(growth_rate>=0,growth_rate,.)
    generate gr_neg = cond(growth_rate<0,growth_rate,.)
    generate gr_null = .
    
    graph bar gr_pos gr_neg gr_null, over(month) over(year) stack blabel(total, format(%9.1f)) ///
        bar(3, color(black)) legend(order(3 "mean of growth rate"))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	65.6 KB
ID:	1701368

    Comment


    • #3
      @William Lisowski

      It is exactly what I want. Thank you so much!

      Comment


      • #4
        See also

        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

        https://www.stata-journal.com/articl...article=gr0049

        Comment

        Working...
        X