Announcement

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

  • Overlay a marginsplot with a two way line graph

    Dear Statalist community,

    Hi! I am struggling to find ways to overlay graph A, the graph of the adjusted predictions of "percent employed" from an OLS diff-in-diff regression I plotted using "margins" and "marginsplot", with graph B, the purely descriptive graph of "percent employed" I plotted using "graph two way (line ...)" into one graph.

    I also wish to change the lines in the purely descriptive graph B into dotted lines once the overlaid graph is achieved.

    Here is the graph A I plotted:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	168.2 KB
ID:	1686714



    Here are the Stata codes I used to plot graph A:

    Code:
    regress employ FEMALE##after edu age age_sq married NumberChild agricultural NorthEast Coastal Central, robust
    
    margins, at(after = (0 1) FEMALE = (0 1))
    
    marginsplot, plot( , label("Men (Aged 17-25)" "Women (Aged 17-25)")) ytitle ("Predicted Percent Employed") xtitle ("Pre-Policy vs. Post-Policy") xlabel (0 "2012" 1 "2014")

    Here is the graph B I plotted:
    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	143.8 KB
ID:	1686715



    Here are the Stata codes I used to graph the purely descriptive graph B:
    Code:
    use "data.dta", clear
    
    gen yearreal = 2012 if year == 3
    replace yearreal = 2014 if year == 5
    
    bysort yearreal FEMALE:egen employ_mean=mean(employ)
    
    graph twoway (line employ_mean yearreal if FEMALE==1) (line employ_mean yearreal if FEMALE == 0), xlabel(2012(2)2014) ytitle("Percent Employed") xtitle("2012 vs. 2014") legend(pos(5) label(1 "women (17-25) percent employed") label(2 "men (17-25) percent employed") cols(1))
    Any suggestions on how I can overlay the above two graphs into one? (Ideally I want to overlay graph B onto graph A and change the two lines in graph B into dotted lines)

    Thank you!
    Last edited by Zixi Li; 25 Oct 2022, 09:43.

  • #2
    -marginsplot- has an "addplot()" option; have you tried that? see
    Code:
    h marginsplot

    Comment


    • #3
      I just tried "addplot":

      If I use
      Code:
      marginsplot, addplot(line employ_mean yearreal if FEMALE==1 || line employ_mean yearreal if FEMALE == 0)
      , the graph is as follows which is wrong:

      Click image for larger version

Name:	Graph3.png
Views:	1
Size:	140.1 KB
ID:	1686738


      If I use
      Code:
      marginsplot, plot( , label("Women (Aged 17-25)" "Men (Aged 17-25)")) ytitle ("Predicted Percent Employed") xtitle ("Pre-Policy vs. Post-Policy") xlabel (0 "2012" 1 "2014"), addplot(graph twoway (line employ_mean yearreal if FEMALE==1) (line employ_mean yearreal if FEMALE == 0), xlabel(2012(2)2014) ytitle("Percent Employed") xtitle("2012 vs. 2014") legend(order(1 "women (17-25) percent employed" 2 "men (17-25) percent employed") cols(1)))
      , it says
      Code:
      invalid 'addplot'
      What should I do? Sorry I am new to "addplot()".

      Comment


      • #4
        I just tried
        Code:
        marginsplot, plot(, label("Women (Aged 17-25)" "Men (Aged 17-25)")) ytitle ("Predicted Percent Employed") xtitle ("Pre-Policy vs. Post-Policy") xlabel (0 "2012" 1 "2014"), addplot(line employ_mean yearreal if FEMALE==1 || line employ_mean yearreal if FEMALE == 0)
        , it still gives the result of "invalid addplot".

        Comment

        Working...
        X