Announcement

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

  • different bar colors in a bar graph

    Hello All,
    I am trying to assess if the income inequality of few developing countries is improving or worsening by taking a data of 2005 and 2023. In my same, I have 9 from SSA countries and 14 from other developing countries. How can I assign different bar colors in a bar graph based on a dummy variable (1 = African country, 0 = non-African),
    I run the following code:
    sort id year
    **change in inequality from 2005- 2023
    by id: gen ineq = gini - gini[_n-18]
    graph bar ineq, over(id, sort(1) lab(angle(45)) ) graphregion(color(white)) scheme(s2color) ytitle("change in inequality")

    I tried to run the following code but didn't work
    graph bar ineq if africa_dummy == 1, over(id, sort(1) lab(angle(45))) bar(1, color(green)) ///
    name(africa, replace) ///
    addplot(graph bar ineq if africa_dummy == 0, over(id, sort(1) lab(angle(45))) bar(1, color(blue))) ///
    legend(off)

    Any suggestion, Thanks
    Attached Files

  • #2
    For full flexibility, you want to switch to twoway bar. graph bar allows you to specify bar colors within the -bar()- option, assuming that you can differentiate bars. There is no data example, so I will use some observations from the census dataset to illustrate some technique. The graph editor is also another possibility. Below, we want to highlight Southern states.

    Code:
    sysuse census, clear
    keep in 6/15
    
    *GEN INDICATOR FOR HIGHLIGHTED BARS
    gen south= region==3
    
    
    *CREATE LOCAL SPECIFYING COLORS
    gen color= cond(south, "stc2", "stc1")
    local colors
    forval i=1/`=_N'{
        local colors "`colors' bar(`i', color(`=color[`i']'))"
    }
    
    *GRAPH
    replace pop= pop/1e+6
    graph hbar pop, over(state, sort(1)) asyvars showyvars nofill ///
    leg(order(3 "South")) `colors' bargap(4) ytitle(Population in Millions)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	37.1 KB
ID:	1777349

    Comment


    • #3
      The reason that the code in #1 did not work can be seen from

      Code:
      help addplot option 
      which explains that the option is reserved for graph twoway.

      I agree with Andrew Musau that twoway bar is more flexible. Nevertheless graph bar can do what is wanted. Let's use a different data example.

      Click image for larger version

Name:	top20.png
Views:	1
Size:	58.8 KB
ID:	1777352


      Code:
      sysuse auto, clear
      sort price
      separate price, by(foreign) veryshortlabel
      graph hbar price? in -20/L , over(make, sort(price) descending) nofill legend(order(1 "Domestic" 2 "Foreign")) ysc(alt) ytitle(Price (USD))
      Naturally I used graph hbar to avoid the awkward use of slanting labels, but the principle is the same either way: different colours are possible if you have different variables, and separate can do that for you.

      See also this paper. which spells out the main point.

      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://journals.sagepub.com/doi/pdf...867X1101100310

      Comment


      • #4
        Ah yes, the separate approach—which I originally learned from Nick's SJ article—completely slipped my mind!

        Comment


        • #5
          Thanks a lot for both of you. The separate approach better works for me.

          Comment


          • #6
            https://journals.sagepub.com/doi/pdf...867X0500500412 documents the otherwise undocumented veryshortlabel option.

            Comment

            Working...
            X