Announcement

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

  • marginsplot and the overlay problem: "Offset for clarity"?

    Dear Statalist,

    There was once a Stata Journal article (http://www.stata-journal.com/article...article=gr0026) that showed how to slightly offset markers in figures, namely like this:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.7 KB
ID:	1435651


    Code:
    clear
    
    input visit str10 group dep sddep n high low
    1 Placebo 16.48 5.28 27 18.51 14.45
    1 Estrogen 13.37 5.56 34 15.28 11.46
    2 Placebo 15.89 6.12 22 18.50 13.28
    2 Estrogen 11.74 6.58 31 14.10 9.38
    3 Placebo 14.13 4.97 17 16.54 11.72
    3 Estrogen 9.13 5.48 29 11.17 7.09
    4 Placebo 12.27 5.85 17 15.11 9.43
    4 Estrogen 8.83 4.67 28 10.60 7.06
    5 Placebo 11.40 4.44 17 13.55 9.25
    5 Estrogen 7.31 5.74 28 9.48 5.14
    end
    
    clonevar x = visit
    
    replace x = cond(group == "Placebo", x - 0.05, x + 0.05)
    
    twoway (connected dep x if group == "Placebo", lpattern(solid) msymbol(D)) ///
     (connected dep x if group == "Estrogen", lpattern(dash) msymbol(S)) ///
     (rcap high low x if group == "Placebo") ///
     (rcap high low x if group == "Estrogen") ///
     , xlabel(1 2 3 4 5) ylab(5(5)20, format(%5.0f)) ///
     xtitle("Visit") ytitle("Depression score") ///
     legend(pos(1) ring(0) col(1) order(1 "Placebo" 2 "Estrogen"))
    Is there an easy solution to do the same for marginsplot, in a situation like this for example?

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	67.5 KB
ID:	1435652


    Code:
    clear
    webuse nhanes2
    regress bpsystol agegrp##sex
    margins agegrp#sex
    marginsplot
    Thank you in advance
    Go

  • #2
    Apart from illustrating how to overcome the overlay problem, this reply shows that you can save the output from margins and create a marginsplot graph using twoway.


    Code:
    clear
    webuse nhanes2
    qui regress bpsystol agegrp##sex
    
    *RUNNING MARGINS AND SAVING RESULT
    qui margins agegrp#sex, saving(myfile)
    
    
    use myfile, clear
    *ADDRESS OVERLAY PROBLEM
    clonevar _mx = _m1
    replace _mx = cond(_m2 == 2, _mx - 0.1, _mx + 0.1)
    
    
    *MARGINSPLOT USING TWOWAY
    twoway (rcap _ci_lb _ci_ub _m1 if _m2==1, sort pstyle(ci) color(green)) ///
    (rcap _ci_lb _ci_ub _mx if _m2==2, sort pstyle(ci) color(orange)) ///
    (connected _margin _m1 if _m2==1, lpattern(solid) msymbol(D)) ///
    (connected _margin _mx if _m2==2, lpattern(dash) msymbol(S)), ///
    title("Adjusted Predictions of agegrp#sex with 95% CIs") ///
    ytitle("Linear Prediction") xla(, valuelabel) graphregion(color(white)) ///
    legend(pos(3) ring(0) col(1) order(1 "Male" 2 "Female"))
    Click image for larger version

Name:	margins.png
Views:	1
Size:	37.2 KB
ID:	1435807

    Last edited by Andrew Musau; 23 Mar 2018, 04:13. Reason: Correcting typo in graph legend

    Comment


    • #3
      Amazing, thanks!

      Comment


      • #4
        My -mplotoffset-, available from SSC, does exactly this.

        Comment


        • #5
          Thank you Nicholas for sharing mplotoffset! Very much appreciated.

          Comment

          Working...
          X