Announcement

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

  • Overlaying two separate twoway line graph based on different sample size

    Dear Stata users,

    I have plotted two twoway line graphs and the code is below:

    preserve
    keep if no_emp_before_close==1
    keep if inrange(event_time,-5,5)
    gcollapse (mean) office_WS, by(event_time)
    label var office_WS "0 employee office before closings"
    grstyle init
    grstyle set plain, dot horizontal grid
    twoway (line office_WS event_time, yaxis(1)) ///
    , graphregion(color(white)) plotregion(color(white)) xline(0, lcolor(black) lpattern(dash)) graphregion(color(white)) name(c, replace) ///
    title("O employee offices before actual closures", size(small)) subtitle("(for kommun closed between 2014-2017)", size(small))
    restore


    preserve
    keep if no_CW_before_close==1
    keep if inrange(event_time,-5,5)
    gcollapse (mean) office_WS, by(event_time)
    label var office_WS "0 CW office before closings"
    grstyle init
    grstyle set plain, dot horizontal grid
    twoway (line office_WS event_time, yaxis(1)) ///
    , graphregion(color(white)) plotregion(color(white)) xline(0, lcolor(black) lpattern(dash)) graphregion(color(white)) name(a, replace) ///
    title("O CW offices before actual closures", size(small)) subtitle("(for kommun closed between 2014-2017)", size(small))
    restore


    I want to overlay these two separate twoway lines in the same graph. In each graph I condition the sample at the first line. Is there any way to do it? Any help would be highly appreciated.

    Thanks

    Zariab Hossain
    Uppsala University

  • #2
    Just generate the means without dropping observations. Here is a start:

    Code:
    bys event_time: egen office_WS1= mean(cond(no_emp_before_close==1 & inrange(event_time,-5,5), office_WS, .))
    label var office_WS1 "0 employee office before closings"
    bys event_time: egen office_WS2= mean(cond(no_CW_before_close==1 & inrange(event_time,-5,5), office_WS, .))
    label var office_WS2 "0 CW office before closings"
    
    tw (line office_WS1 event_time, sort) (line office_WS2 event_time, sort)

    Comment


    • #3
      Thanks a lot Andrew. It solves the problem.

      Comment

      Working...
      X