I'm wondering if I could produce the attached graph (stacked area plot) using Stata?
I used
twoway area
Groups overlaid each other. Each category did not start from the end of previous category. (my graph)
* Example generated by -dataex-. To install: ssc install dataex clear input int year long(pop1 pop2 pop3 pop4 pop5 pop6 pop7 pop8) int pop9 2001 6530349 4763615 3571469 1906274 1503461 473668 321538 201743 2584 2002 6580807 4817774 3653123 1928512 1511567 474152 324627 202251 2397 2003 6620715 4873809 3743121 1952741 1520399 478534 327357 201725 2336 2004 6650735 4927149 3829970 1979542 1528189 483178 328940 202663 2356 2005 6693206 4989246 3918494 2011207 1538804 486202 331399 205905 2381 2006 6742690 5061266 4007992 2050581 1552529 489302 335170 209057 2379 2007 6834156 5153522 4111018 2106139 1570619 493262 342644 213748 2514 2008 6943461 5256375 4219505 2171700 1588665 498568 348368 219874 2683 2009 7053755 5371934 4328771 2240250 1608902 504353 354785 226027 2876 2010 7144292 5461101 4404744 2290845 1627322 508847 361766 229778 3055 2011 7218529 5537817 4476778 2353409 1639614 511483 367985 231292 3117 2012 7304244 5651091 4568687 2425507 1656725 511724 376539 235915 3033 2013 7404032 5772669 4652824 2486944 1671488 512231 383257 241722 2962 2014 7508353 5894917 4719653 2517608 1686945 513621 388799 242894 2896 2015 7616168 6022322 4777692 2540672 1700668 515117 395813 244692 2851 2016 7732858 6173172 4845152 2555978 1712843 517514 403104 245678 4608 2017 7867936 6321606 4927629 2574193 1723923 522410 412025 247517 4621 2018 7980168 6462019 5009424 2594181 1736527 528298 420379 247058 4634 2019 8089817 6596039 5094510 2621509 1751963 534457 426704 245929 4643 end label var pop1 "New South Wales" label var pop2 "Victoria" label var pop3 "Queensland" label var pop4 "Western Australia" label var pop5 "South Australia" label var pop6 "Tasmania" label var pop7 "Australian Capital Territory" label var pop8 "Northern Territory" label var pop9 "Other Territories"
* ssc inst mycolours * https://www.statalist.org/forums/forum/general-stata-discussion/general/1568168-mycolours-package-available-on-ssc mycolours clonevar show1 = pop1 forval j = 2/9 { local k = `j' - 1 gen show`j' = show`k' + pop`j' _crcslbl show`j' pop`j' } local toshow forval j = 9(-1)1 { local toshow `toshow' show`j' } twoway area `toshow' year, xla(2001 2019 2005(5)2015) xtitle("") //// col("`OK9'" "`OK8'" "`OK7'" "`OK6'" "`OK5'" "`OK4'" "`OK3'" "`OK2'" "`OK1'") /// yla(0 5e6 "5" 1e7 "10" 15e6 "15" 2e7 "20" 25e6 "25", ang(h)) legend(col(1) pos(3)) ytitle(Population (millions)) name(G0, replace)
* ssc install combineplot * https://www.statalist.org/forums/forum/general-stata-discussion/general/3055-combineplot-available-on-ssc combineplot (pop*) year, combine(name(G1, replace)): line @y @x , yla(, ang(h)) xtitle("")
sort year forval j = 1/9 { gen pop`j'_s = pop`j'/pop`j'[10] _crcslbl pop`j'_s pop`j' } combineplot (pop1_s-pop8_s) year, combine(name(G2, replace) note(2010=100)): /// line @y @x , yla(0.8 "80" 0.9 "90" 1 "100" 1.1 "110" 1.2 "120", ang(h)) xtitle("") ysc(r(0.8 1.2)) yli(1, lc(gs12) lw(thin))
clear set obs 16 set seed 1234 gen year= 1999+_n forvalues i= 1/4{ gen CNTRY`i'= rnormal(10000, `i'500) } *GENERATE CUMULATIVE VALUES gen _CNTRY1= CNTRY1 forvalues i= 2/4{ local j= `i'-1 gen _CNTRY`i'= _CNTRY`j' + CNTRY`i' } *REVERSE ORDER: PLOT LAST CUMULATIVE CATEGORY FIRST twoway (area _CNTRY4 _CNTRY3 _CNTRY2 _CNTRY1 year, graphregion(color(white))/// yla(0(10000)50000) xlab(2000(5) 2015) color(gs4 gs8 gs12 gs 16))
Leave a comment: