Announcement

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

  • Stacked area plot

    Hi Statalist,

    I'm wondering if I could produce the attached graph (stacked area plot) using Stata?

    Click image for larger version

Name:	stacked area plot.PNG
Views:	0
Size:	0
ID:	1434466


    I used
    twoway area
    and played with different options but I could not produce exactly the same graph.

    Groups overlaid each other. Each category did not start from the end of previous category. (my graph)
    Click image for larger version

Name:	my graph.png
Views:	0
Size:	0
ID:	1434465
    Last edited by Masoumeh Sanagou; 14 Mar 2018, 18:26.

  • #2
    graphs are attached here.
    Attached Files

    Comment


    • #3
      The stack option is part of the graph command but you can be able to recreate a stacked area plot with twoway. However, you should communicate to your readers that the graphs are stacked (and not overlaid), otherwise the y-axis makes no sense. The trick is to create cumulative categories and plot the last category first. Here is an example

      Code:
      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))
      Click image for larger version

Name:	stacked_area.png
Views:	1
Size:	41.7 KB
ID:	1434548

      Comment

      Working...
      X