Announcement

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

  • Graph Population pyramid

    Dears,

    I am trying to make a population pyramid graph. I did it using the following command:

    Code:
    #delimit ;
    twoway bar maxmaletotal age_gr ,  horizontal bfc("20 100 131"*0.6)  ||
    bar maxfemtotal age_gr , horizontal bfc("226 174 156"*0.6)  ||
    scatter age_gr zero, mlabel(age_gr) mlabcolor(black) msymbol(none)
    note("Source: Own Survey", span)
    xtitle("Number of people ") ytitle("Age Group Number")
    ytitle("") yscale(noline) ylabel(none)
    xlabel( -50 "50" -40 "40" -30 "30" -20 "20" -10 "10" 10(10)52)
    legend(off) text(15 -8 "Male") text(15 8 "Female");
    #delimit cr
    here the result
    Click image for larger version

Name:	Graph pyramid.png
Views:	1
Size:	14.8 KB
ID:	1418064


    As you can see the gr is not perfect and I have some difficulties to make it as I whish.
    What I would like to change is the following:
    1) remove those vertical lines that are showed in some bars ( I don't know why I get them);
    2) have the label of group age as in this gr (in the middle but with space from the 2 charts)
    Click image for larger version

Name:	graph4-8.jpg
Views:	1
Size:	36.8 KB
ID:	1418065

    3) modify the intensity of the colour (what I did in my command does not produce any change in colours)
    4) and if possible do it by regions.

    I copy the data of 3 regions ( I have five).
    Can anyone help me?

    Thanks in advance

    Federica
    Regions age_gr zero maletotal femtotal
    A 19-24 0 -3
    A 25-29 0 -17 2
    A 30-34 0 -23 2
    A 35-39 0 -28 12
    A 40-44 0 -39 26
    A 45-49 0 -37 28
    A 50-54 0 -45 52
    A 55-59 0 -44 52
    A 60-64 0 -37 42
    A 65-69 0 -14 10
    A 70-74 0 -1 6
    A 85 and more 0 -1
    B 19-24 0 -1
    B 25-29 0 -3
    B 30-34 0 -5 4
    B 35-39 0 -8 2
    B 40-44 0 -9 4
    B 45-49 0 -17 10
    B 50-54 0 -4 16
    B 55-59 0 -11 10
    B 60-64 0 -10 6
    B 65-69 0 -4 4
    C 25-29 0 -3
    C 30-34 0 -9
    C 35-39 0 -16 16
    C 40-44 0 -16 6
    C 45-49 0 -32 16
    C 50-54 0 -40 6
    C 55-59 0 -26 8
    C 60-64 0 -18 10
    C 65-69 0 -7
    C 85 and more 0 -6

  • #2
    Not what you want, but my advice is don't do this.

    The pyramid form, although popular, makes it hard work to see subtle differences in the sex ratio, which exist almost universally and are usually interesting and important.

    Also small totals, here for older groups, are hard to see as such.

    The stacked design compounds those design faults to add comparison with varying baselines on either side to the cognitive load.

    I'd be happy to illustrate alternatives, but the data example as presented here is hard to read (you don't use dataex (SSC) as requested) and is in mangled form any way (there isn't a complete breakdown).
    Last edited by Nick Cox; 13 Nov 2017, 10:25.

    Comment


    • #3
      Dear Nick,

      thank you for your advise.
      I will check how to copy the data in the proper manner and I will be happy if you could offer me some alternatives to illustrate the info I have got.
      Best

      Federica

      Comment


      • #4
        Is this ok?

        Code:
        clear
        input float(Regions age_gr maletotal femtotal zero)
        1  1  -3  . 0
        1  2 -17  . 0
        1  3 -23  . 0
        1  4 -28  . 0
        1  5 -39  . 0
        1  6 -37  . 0
        1  7 -45  . 0
        1  8 -44  . 0
        1  9   . 42 0
        1 10 -14  . 0
        1 11  -1  . 0
        1 14  -1  . 0
        2  1  -1  . 0
        2  2  -3  . 0
        2  3   .  4 0
        2  4  -8  . 0
        2  5   .  4 0
        2  6   . 10 0
        2  7   . 16 0
        2  8 -11  . 0
        2  9   .  6 0
        2 10  -4  . 0
        3  2  -3  . 0
        3  3  -9  . 0
        3  4   . 16 0
        3  5 -16  . 0
        3  6 -32  . 0
        3  7 -40  . 0
        3  8 -26  . 0
        3  9 -18  . 0
        3 10  -7  . 0
        3 14  -6  . 0
        end
        label values Regions reg
        label def reg 1 "A", modify
        label def reg 2 "B", modify
        label def reg 3 "C", modify
        label values age_gr age_gr
        label def age_gr 1 "19-24", modify
        label def age_gr 2 "25-29", modify
        label def age_gr 3 "30-34", modify
        label def age_gr 4 "35-39", modify
        label def age_gr 5 "40-44", modify
        label def age_gr 6 "45-49", modify
        label def age_gr 7 "50-54", modify
        label def age_gr 8 "55-59", modify
        label def age_gr 9 "60-64", modify
        label def age_gr 10 "65-69", modify
        label def age_gr 11 "70-74", modify
        label def age_gr 14 "85 and more", modify

        Comment


        • #5
          If I understand the data correctly your populations are either male or female in each age group. So I presume I don't understand your data at all.

          Comment


          • #6
            Dear Nick,

            I realised that the higher age group presents some problem but this is not a big concern ( For the purpose of this study I can also group all those that are 70 and above). Probably I was not clear in my original e-mail. In the graph I made I want to make a representation of age gr by sex but with a different layout (I don't understand why I get this vertical line in some bars). However, the vars I have copied are not the original ones, I have created the maletot, femaletot and age_gr . Anyway I just would like to understand how to improve the layout of my graph.
            Thanks
            Federica

            Comment


            • #7
              Sorry, but I don't see an explanation of the bizarre data example in #4 or that you have given a reproducible example of your problem in #1. So I can't add more.

              Comment


              • #8
                Ok I think I got the issue. By using the data I copied you cannot reproduce the gr I posted. This is because I only copied part of them. Additionally the graph plots the pooled info, that is the 5 Regions together. If it might help I can copy the 1248 obs. Thanks

                Comment


                • #9
                  In #1 you have 14 age groups for males and females. So, your data should be reducible to a set of results with 14 observations. With 3 regions that could be 42 observations. Again it's difficult to suggest exactly how to do that without knowing what constitutes a typical observation, but collapse and contract may be helpful.

                  Comment


                  • #10
                    Sorry ...I did not notice that there was something weird in the data I copied.

                    here again the data. I have missing value for some age_gr. Using this info and the command above it will reproduce exactly my figure.


                    Code:
                    input float(Regions age_gr maletotal femtotal zero)
                    1 1 -3 . 0
                    1 2 -17 2 0
                    1 3 -23 2 0
                    1 4 -28 12 0
                    1 5 -39 26 0
                    1 6 -37 28 0
                    1 7 -45 52 0
                    1 8 -44 52 0
                    1 9 -37 42 0
                    1 10 -14 10 0
                    1 11 -1 6 0
                    1 14 -1 . 0
                    2 1 -1 . 0
                    2 2 -3 . 0
                    2 3 -5 4 0
                    2 4 -8 2 0
                    2 5 -9 4 0
                    2 6 -17 10 0
                    2 7 -4 16 0
                    2 8 -11 10 0
                    2 9 -10 6 0
                    2 10 -4 4 0
                    3 2 -3 . 0
                    3 3 -9 . 0
                    3 4 -16 16 0
                    3 5 -16 6 0
                    3 6 -32 16 0
                    3 7 -40 6 0
                    3 8 -26 8 0
                    3 9 -18 10 0
                    3 10 -7 . 0
                    3 14 -6 . 0
                    4 1 -2 . 0
                    4 2 -9 . 0
                    4 3 -14 4 0
                    4 4 -20 6 0
                    4 5 -19 2 0
                    4 6 -29 6 0
                    4 7 -26 4 0
                    4 8 -34 4 0
                    4 9 -21 4 0
                    4 10 -3 . 0
                    4 11 -4 . 0
                    4 12 -3 . 0
                    4 13 -1 . 0
                    4 14 -4 . 0
                    5 1 -5 2 0
                    5 2 -24 . 0
                    5 3 -32 16 0
                    5 4 -42 30 0
                    5 5 -41 26 0
                    5 6 -34 20 0
                    5 7 -33 28 0
                    5 8 -33 8 0
                    5 9 -16 4 0
                    5 10 -4 4 0
                    5 11 . 2 0
                    5 14 -1 . 0
                    end
                    label values Regions reg
                    label def reg 1 "A", modify
                    label def reg 2 "B", modify
                    label def reg 3 "C", modify
                    label def reg 4 "D", modify
                    label def reg 5 "E", modify
                    label values age_gr age_gr
                    label def age_gr 1 "19-24", modify
                    label def age_gr 2 "25-29", modify
                    label def age_gr 3 "30-34", modify
                    label def age_gr 4 "35-39", modify
                    label def age_gr 5 "40-44", modify
                    label def age_gr 6 "45-49", modify
                    label def age_gr 7 "50-54", modify
                    label def age_gr 8 "55-59", modify
                    label def age_gr 9 "60-64", modify
                    label def age_gr 10 "65-69", modify
                    label def age_gr 11 "70-74", modify
                    label def age_gr 12 "75-79", modify
                    label def age_gr 13 "80-84", modify
                    label def age_gr 14 "85 and more", modify
                    and here the collapsed dataset

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float age_gr long(maletotal femtotal) float zero
                     1  -12   6 0
                     2  -57  18 0
                     3  -96  87 0
                     4 -147 147 0
                     5 -156 156 0
                     6 -189 189 0
                     7 -201 201 0
                     8 -189 189 0
                     9 -135 135 0
                    10  -41  31 0
                    11   -8   5 0
                    12   -3   0 0
                    13   -1   0 0
                    14  -12   0 0
                    end
                    label values age_gr age_gr
                    label def age_gr 1 "19-24", modify
                    label def age_gr 2 "25-29", modify
                    label def age_gr 3 "30-34", modify
                    label def age_gr 4 "35-39", modify
                    label def age_gr 5 "40-44", modify
                    label def age_gr 6 "45-49", modify
                    label def age_gr 7 "50-54", modify
                    label def age_gr 8 "55-59", modify
                    label def age_gr 9 "60-64", modify
                    label def age_gr 10 "65-69", modify
                    label def age_gr 11 "70-74", modify
                    label def age_gr 12 "75-79", modify
                    label def age_gr 13 "80-84", modify
                    label def age_gr 14 "85 and more", modify
                    I hope it is better now.

                    Comment


                    • #11
                      Here's one approach: superimpose bars and use transparency (Stata 15) to see overlap.

                      I was puzzled by my own graph because I didn't see at first that males and females have equal numbers in six age groups.

                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input float age_gr long(maletotal femtotal) float zero
                       1  -12   6 0
                       2  -57  18 0
                       3  -96  87 0
                       4 -147 147 0
                       5 -156 156 0
                       6 -189 189 0
                       7 -201 201 0
                       8 -189 189 0
                       9 -135 135 0
                      10  -41  31 0
                      11   -8   5 0
                      12   -3   0 0
                      13   -1   0 0
                      14  -12   0 0
                      end
                      label values age_gr age_gr
                      label def age_gr 1 "19-24", modify
                      label def age_gr 2 "25-29", modify
                      label def age_gr 3 "30-34", modify
                      label def age_gr 4 "35-39", modify
                      label def age_gr 5 "40-44", modify
                      label def age_gr 6 "45-49", modify
                      label def age_gr 7 "50-54", modify
                      label def age_gr 8 "55-59", modify
                      label def age_gr 9 "60-64", modify
                      label def age_gr 10 "65-69", modify
                      label def age_gr 11 "70-74", modify
                      label def age_gr 12 "75-79", modify
                      label def age_gr 13 "80-84", modify
                      label def age_gr 14 "85 and more", modify
                      
                      set scheme s1color 
                      
                      replace maletotal = -maletotal 
                      
                      twoway bar maletotal age_gr, bcolor(blue%20) horizontal ///
                      || bar femtotal age_gr, bcolor(orange%20) horizontal ///
                      yla(1/14, ang(h) noticks valuelabel) legend(order(1 "males" 2 "females")) ///
                      ytitle(Age group)
                      Click image for larger version

Name:	pyramid2.png
Views:	1
Size:	23.5 KB
ID:	1418267

                      Comment


                      • #12
                        Dear Nick

                        thank you for your suggestion. But in this grit is might not be clear whether for the age_gr 4-9 females and males are equal or females>males, that is why I preferred to have the pyramid.
                        But concerning my previous questions, is it possible to modify the intensity of the colour I made? I thought this bfc("226 174 156"*0.6) was correct but the colour does not change. Probably is not the right command.

                        Thanks

                        Comment


                        • #13
                          Sorry; don't understand "in this grit".

                          But not so. For your example you see a mix of colours for precisely those groups with equal numbers of males and females. The point about transparency is that the smaller group is never occluded. You can see where #males > #females and you would see where the opposite is true if there were any such cases.

                          I don't know why

                          Code:
                          bfc("226 174 156"*0.6)
                          doesn't change the colour.

                          Comment


                          • #14
                            I mean "in this graph might not".... I know that the code I wrote doesn't change the colour intensity that is why I was asking an help...but never mind.

                            Comment


                            • #15
                              I am agreeing that it is a puzzle. You could report it to StataCorp technical support.

                              Comment

                              Working...
                              X