Announcement

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

  • Population pyramid, graph hbar and blabel

    Dear Stata users,

    I want to use graph hbar to draw a population pyramid. Regular procedure is to generate a variable that equals to its negative (for example negmales in my dataset). When I graph hbar and add labels to bars, labels that in left are unsurprisingly draw as negative numbers. So my question is how to make them be drawn as positive numbers. The following example is a modified dataset borrowed from Lawrence Hamilton's Statistics with STATA (sic).

    Code:
    clear
    input int year byte agegroup float(negmales females)
    2006 1 -7818 7560
    2006 2 -3487 3396
    2006 3 -2917 2710
    2006 4 -5011 4768
    2006 5 -3489 2949
    2006 6 -1902 1711
    2006 7  -921 1032
    2006 8  -259  404
    2006 9   -13   50
    end
    label values agegroup age
    label def age 1 "0~16", modify
    label def age 2 "17~25", modify
    label def age 3 "26~35", modify
    label def age 4 "36~45", modify
    label def age 5 "46~55", modify
    label def age 6 "56~65", modify
    label def age 7 "66~75", modify
    label def age 8 "76~85", modify
    label def age 9 "85~91", modify
    
    graph hbar (sum) negmales females, over(agegroup, descending gap(0)) ylabel(-10000 "10000" -5000 "5000" 0 5000 10000) stack blabel(bar)
    Click image for larger version

Name:	pyramid.png
Views:	1
Size:	62.4 KB
ID:	1637428


  • #2
    There is no option to modify bar labels in graph. You will have to use a graph editor hack.

    Code:
    clear
    input int year byte agegroup float(negmales females)
    2006 1 -7818 7560
    2006 2 -3487 3396
    2006 3 -2917 2710
    2006 4 -5011 4768
    2006 5 -3489 2949
    2006 6 -1902 1711
    2006 7  -921 1032
    2006 8  -259  404
    2006 9   -13   50
    end
    label values agegroup age
    label def age 1 "0~16", modify
    label def age 2 "17~25", modify
    label def age 3 "26~35", modify
    label def age 4 "36~45", modify
    label def age 5 "46~55", modify
    label def age 6 "56~65", modify
    label def age 7 "66~75", modify
    label def age 8 "76~85", modify
    label def age 9 "85~91", modify
    
    graph hbar (sum) negmales females, over(agegroup, descending gap(0)) ylabel(-10000 "10000" -5000 "5000" 0 5000 10000) stack blabel(bar)
    
    forval i=1/`.Graph.plotregion1.barlabels.arrnels'{
        local val "`.Graph.plotregion1.barlabels[`i'].text[1]'"
        gr_edit .plotregion1.barlabels[`i'].text[1]=ustrregexra("`val'", "-", "")
    }
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	63.5 KB
ID:	1637437

    Comment


    • #3
      I learned this trick from Nick Cox.

      Code:
      clear
      input int year byte agegroup float(negmales females)
      2006 1 -7818 7560
      2006 2 -3487 3396
      2006 3 -2917 2710
      2006 4 -5011 4768
      2006 5 -3489 2949
      2006 6 -1902 1711
      2006 7  -921 1032
      2006 8  -259  404
      2006 9   -13   50
      end
      label values agegroup age
      label def age 1 "0~16", modify
      label def age 2 "17~25", modify
      label def age 3 "26~35", modify
      label def age 4 "36~45", modify
      label def age 5 "46~55", modify
      label def age 6 "56~65", modify
      label def age 7 "66~75", modify
      label def age 8 "76~85", modify
      label def age 9 "85~91", modify
      
      gen males = abs(negmales)
      
      twoway bar negmales age, horizontal col(navy) || ///
             bar females age, horizontal col(maroon) || ///
             scatter age negmales, m(i) mc(navy) mlab(males) mlabp(9) || ///
             scatter age females, m(i) mc(maroon) mlab(females) mlabp(3) ///
             ylab(1(1)9, val angle(horizontal) noticks) ytitle("Age groups") ///
             xlab(-10000 "10000" -5000 "5000" 0 5000 10000) ///
             leg(order(1 "Males" 2 "Females"))
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	381.0 KB
ID:	1637453

      Last edited by Fei Wang; 21 Nov 2021, 05:28.

      Comment


      • #4
        Thank you Andrew Musau Fei Wang. I used Graph Editor to delete the minus sign. Fei Wang’s solution is good, although it seems a little complex. By the way, Nick Cox himself doesn't like pyramid graph. https://www.statalist.org/forums/for...lation-pyramid

        Comment


        • #5
          Oh I didn't realize that. The trick in #3 is to add invisible scatters with labels to bars, which I learnt from a post by Nick. I like it very much because it makes twoway graphs very flexible.

          Comment


          • #6
            Some extra points are raised in discussion within https://www.statalist.org/forums/for...oway-bar-graph

            Comment

            Working...
            X