Announcement

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

  • vary marker color in marginsplot

    Hi, I'm creating a scatterplot using marginsplot and would like the color of the markers/datapoints to vary based on the levels of the variable I'm plotting. For example,
    Code:
    use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear
    
    regress standlrt i.girl i.schav
    margins schav
    marginsplot, recast(scatter) plotopts(mcolor("red"))
    creates a marginsplot by the school average LRT score (schav) and I would like it if the "low" category was blue, the "mid" category was purple, and the "high" category was red instead of all being red markers. I've seen examples on the forum for how to do this with scatterplots more generally, including the sepscatter user-written command, but as far as I can tell none would work directly with marginsplot.

    Note also that I am using Stata 13.
    Graph.gph

  • #2
    Marginsplot is nothing more than a twoway graph. If you need to customize the graph, then save the output from margins and use graph twoway to plot. This solution is based on your example. Initially, the marginsplot looks as follows where we are unable to specify more than one color for the markers.


    Click image for larger version

Name:	marginsplot1.png
Views:	1
Size:	50.9 KB
ID:	1417227


    The solution using graph twoway proceeds as follows:

    Code:
    use "http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta", clear
    regress standlrt i.girl i.schav
    *//SAVE THE OUTPUT FROM MARGINS
    margins schav, saving(myfile)
    clear
    use myfile
    *//MARGINSPLOT USING GRAPH TWOWAY
    twoway (rcap _ci_lb _ci_ub _m1, sort pstyle(ci) color(navy))///
    (scatter _margin _m1 if _m1==1, sort mc("blue"))///
    (scatter _margin _m1 if _m1==2, sort mc("purple"))///
    (scatter _margin _m1 if _m1==3, sort mc("red")),///
    legend(off) title("Predictive Margins of schav with 95% CIs")///
    ytitle("Linear Prediction") xla(1/3, valuelabel)

    Click image for larger version

Name:	marginsplot2.png
Views:	1
Size:	51.8 KB
ID:	1417228
    Last edited by Andrew Musau; 06 Nov 2017, 03:25.

    Comment

    Working...
    X