Announcement

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

  • Stacked Area Chart for Panel Data

    Hi,

    Is there any way to create a stacked area chart as below:

    Click image for larger version

Name:	stacked.png
Views:	1
Size:	241.5 KB
ID:	1607112

  • #2
    Create the graphs one by one, saving each and then use graph combine to merge them.

    Code:
    preserve
    keep if country=="Australia"
    *GENERATE GRAPH AND SAVE
    restore, preserve
    keep if country=="Austria"
    *GENERATE GRAPH AND SAVE
    restore, preserve
    You can use a loop for the above. There are plenty of examples on how to create these graphs, e.g.,

    https://www.statalist.org/forums/for...cked-area-plot
    https://www.statalist.org/forums/for...ked-area-chart

    Also, you may want to take a look at the community-contributed command stackedcount from SSC, authored by Cameron Campbell. More on it at https://www.statalist.org/forums/for...ed-area-graphs
    Last edited by Andrew Musau; 02 May 2021, 12:20.

    Comment


    • #3
      That worked! Thank you so much

      Comment


      • #4
        I wouldn't use graph oombine here, but a by() option.

        Code:
         clear 
        set obs 100 
        gen country = word("Australia Austria Belgium Canada", ceil(_n/25))
        set seed 2803 
        
        forval j = 1/4 { 
            gen y`j' = `j' * runiform()
        }
        
        egen Y = rowtotal(y*)
        
        gen p1 = 100 * y1/Y 
        gen P2 = 100 * (y1 + y2) / Y 
        gen P3 = 100 * (y1 + y2 + y3) / Y 
        gen P4 = 100 
        
        bysort country : gen year = _n + 1979
        
        twoway area p1 year || rarea p1 P2 year || rarea P2 P3 year || rarea P3 P4 year, ///
        by(country, note(""))  ytitle(percent of total) xla(1980(5)2005) xtitle("")  yla(0(25)100, ang(h)) legend(order(4 "4" 3 "3" 2 "2" 1 "1") row(1))

        Comment


        • #5
          That works too! Thanks Nick!

          Comment

          Working...
          X