Announcement

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

  • Creating a stacked bar graph without Stata summing the two variables

    Hello all,

    I'm doing a similar graph to the one in this paper - https://www.imf.org/external/pubs/ft...14/wp14143.pdf (page 8).

    Now I have two variables - current share of working age population (SWAP) and peak SWAP. I'm trying to create a stack bar graph with the current SWAP (one colour) and Peak Swap, another colour (note: some countries have already reached peak swap) but what Stata is doing is adding the two variable values together...

    Here is my data:


    Code:
    clear
    input str32 Country double(SWAP_2015 Peak_Swap)
    "Burundi" .5272741 .64316753
    "Comoros" .56933048 .65728474
    "Djibouti" .63078075 .67825481
    "Eritrea" .54583428 .66373454
    "Ethiopia" .55075882 .6752352
    "Kenya" .55288495 .64550963
    "Madagascar" .55452848 .63910625
    "Malawi" .51401631 .63617378
    "Mauritius" .71114899 .71114899
    "Mayotte" .5456481 .64491902
    "Mozambique" .51347798 .64488812
    "Réunion" .66122162 .66122162
    "Rwanda" .56158108 .67136051
    "Seychelles" .69676583 .69676583
    "Somalia" .50470914 .65737982
    "South Sudan" .54449607 .66458788
    "Uganda" .49420458 .64950415
    "United Republic of Tanzania" .51599338 .6323585
    "Zambia" .51176454 .62643488
    "Zimbabwe" .55435132 .66353703
    "Angola" .55435132 .6519489
    "Cameroon" .51655539 .64758323
    "Central African Republic" .50036096 .65746906
    "Chad" .54271729 .65729875
    "Congo" .57091977 .63371238
    "Democratic Republic of the Congo" .49828929 .64958036
    "Equatorial Guinea" .53708436 .65976741
    "Gabon" .51043131 .6508223
    "Sao Tome and Principe" .5785317 .6444935
    "Algeria" .57768201 .67315506
    "Egypt" .54288116 .64962904
    "Libya" .62608548 .70267025
    "Morocco" .6552325 .66609702
    "Sudan" .61619461 .65490064
    "Tunisia" .65626445 .69051197
    "Western Sahara" .66609702 .72124893
    "Botswana" .56168695 .69203171
    "Lesotho" .69051197 .6738113
    "Namibia" .71342439 .66139968
    "South Africa" .65106795 .68867583
    "Swaziland" .64403688 .68244618
    "Benin" .59785107 .66209546
    "Burkina Faso" .59784735 .65626512
    "Cabo Verde" .65727404 .70021433
    "Côte d'Ivoire" .59062216 .64193763
    "Gambia" .53359297 .67006621
    "Ghana" .54950427 .66060156
    "Guinea" .52034628 .65138075
    "Guinea-Bissau" .65768876 .66073095
    "Liberia" .54498757 .65465113
    "Mali" .51501969 .65206248
    "Mauritania" .57788729 .65308729
    "Niger" .54404584 .64792137
    "Nigeria" .56038811 .65813173
    "Senegal" .5468282 .63063251
    "Sierra Leone" .4995417 .68304356
    "Togo" .56773692 .64915847
    end

  • #2
    This is the graph I would make. The first line creates a new variable (axis) that sorts the countries by SWAP in 2015. Sorting countries this way often makes the graph much more informative. It requires the egenmore package that can be installed by typing in Stata ssc install egenmore. The second line creates the graph.
    Code:
    egen axis = axis(SWAP_2015), label(Country)
    twoway pcarrow SWAP_2015 axis Peak_Swap axis,    ///
           xlab(1/56, valuel angle(35) labsize(*.5)) ///
           xtitle("")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	71.4 KB
ID:	1380430
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      I'd do something loosely similar to Maarten's graph. I like arrows

      SJ-9-4 gr0041 . . . . . . Speaking Stata: Paired, parallel, or profile plots
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q4/09 SJ 9(4):621--639 (no commands)
      explores paired, parallel, and profile plot making for
      changes, correlations, and other comparisons between
      variables

      SJ-5-2 gr0015 . . . . . . . . Stata tip 21: The arrows of outrageous fortune
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q2/05 SJ 5(2):282--284 (no commands)
      tip for using graph twoway pcarrow for graphing changes
      over time
      but here as quite often very short arrows for zero or minimal change are disconcerting.

      Here labmask is from the Stata Journal and is my preferred way to do what axis() does. (I am the author of both. Why it's my preferred way is not especially important: it's just a personal preference that what it does should be available as a command.)

      Naturally appropriate axis and graph titles and other details need to be fixed, and there are other ways to do it. But stacked bar charts sound utterly wrong in principle: these are not additive quantities.

      Code:
      sort SWAP_2015
      gen obs = _n
      labmask obs, values(Country)
      
      scatter obs SWAP_2015, ms(Oh) ///
      || scatter obs Peak_Swap, ms(+) ///
      ||rspike SWAP_2015 Peak_Swap obs, horizontal ///
      yla(1/57, ang(h) valuel noticks labsize(*.5) grid glc(gs12) glw(vvthin)) ///
      ysize(8) ytitle("") legend(order(1 "2015" 2 "Peak")) ///
      xla(0.5 "0.5" 0.6 "0.6" 0.7 "0.7" 0.55 "0.55" 0.65 "0.65")
      Click image for larger version

Name:	rooney2.png
Views:	1
Size:	27.9 KB
ID:	1380438

      Last edited by Nick Cox; 27 Mar 2017, 06:04.

      Comment

      Working...
      X