Announcement

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

  • Overlaying symbols and lines in twoway legend

    I want to estimate separate regressions and then create a "marginsplot" (using the twoway command) with the groups held at the grand means for the sample.

    Here is my code using auto.dta:

    sysuse auto.dta, clear

    foreach var in trunk turn weight {
    sum `var'
    scalar `var'1 = r(mean)
    }

    foreach n in 0 1 {
    reg price mpg trunk turn weight if for==`n'
    margins, at(mpg=(10(5)30) trunk=`=trunk1' turn=`=turn1' weight=`=weight1') ///
    saving(marg`n'.dta, replace)
    }

    foreach n in 0 1 {
    use marg`n'.dta, replace
    gen for = `n'
    save, replace
    }

    use marg0.dta, replace

    append using marg1.dta

    twoway ///
    (scatter _margin _at1 if for==0) (scatter _margin _at1 if for==1) ///
    (line _margin _at1 if for==0) (line _margin _at1 if for==1) ///
    (rcap _ci_ub _ci_lb _at1 if for==0) (rcap _ci_ub _ci_lb _at1 if for==1), ///
    legend(order(1 3 2 4) label(1 " ") label(3 "Foreign") label(2 " ") ///
    label(4 "Domestic") pos(6) col(2))

    Here is the graph:

    Click image for larger version

Name:	example_graph_5.png
Views:	1
Size:	78.8 KB
ID:	1551850


    My question is: how do I get the marker symbol and the line for each group to appear on top of each other in the legend (as they would in a plot produced by the 'marginsplot' command)?

  • #2
    Bill,

    This doesn't overlay so perhaps it's not a good solution.
    Code:
    twoway ///
    (scatter _margin _at1 if for==0) (scatter _margin _at1 if for==1) ///
    (line _margin _at1 if for==0) (line _margin _at1 if for==1) ///
    (rcap _ci_ub _ci_lb _at1 if for==0) (rcap _ci_ub _ci_lb _at1 if for==1), ///
    legend(order(1 3 2 4) colfirst label(1 " ") label(3 "Foreign") label(2 " ") ///
    label(4 "Domestic") pos(6) col(2))
    This simply add the colfirst suboption of the legend option.

    Why not use marginsplot?

    Lance

    Edit: tried it using marginsplot and realized it probably wasn't what you wanted.
    Last edited by Lance Erickson; 07 May 2020, 17:22.

    Comment


    • #3
      Use -connect- rather than -scatter- & -line-:
      Code:
      twoway connect _margin _at1 if for==0, lc(blue) mc(blue) /// 
       || connect  _margin _at1 if for==1, lc(green) mc(green) ///
       || rcap _ci_ub _ci_lb _at1 if for==0 , lc(blue) /// 
       || rcap _ci_ub _ci_lb _at1 if for==1 , lc(green) /// 
       || , legend(order( 1 "Domestic" 2 "Foreign" ) pos(6))

      Comment


      • #4
        In addition to Scott's advice:
        • don't use the fixed file names for temporary needs,
        • don't use current directory for saving temporary data, and
        • don't use option replace when posting an example to a website. Someone just may loose the data if happens to have the file with same name and run the code without checking.
        I have also encountered an option replace after the command use in your code. Not sure why it works.
        The official option is clear. Perhaps that is a rudiment of older Stata's syntax.

        Code:
        sysuse auto.dta, clear
        
        foreach var in trunk turn weight {
            sum `var'
            scalar `var'1 = r(mean)
        }
        
        foreach n in 0 1 {
            tempfile tmp`n'
            reg price mpg trunk turn weight if for==`n'
            margins, at(mpg=(10(5)30) trunk=`=trunk1' turn=`=turn1' weight=`=weight1') ///
            saving(`tmp`n'', replace)
        }
        
        foreach n in 0 1 {
            use `tmp`n'', clear
            gen for = `n'
            save, replace
        }
        
        use `tmp0', clear
        append using `tmp1'
        
        local c0 "maroon"
        local c1 "navy"
        
        twoway ///
            (scatter _margin _at1 if for==0, connect(line) color(`c0')) ///
            (scatter _margin _at1 if for==1, connect(line) color(`c1')) ///
            (rcap _ci_ub _ci_lb _at1 if for==0 , color(`c0')) ///
            (rcap _ci_ub _ci_lb _at1 if for==1, color(`c1')), ///
            legend(pos(6) col(2) order(1 2) ///
            label(1 "Foreign") label(2 "Domestic") )
        Click image for larger version

Name:	Graph9.png
Views:	1
Size:	61.5 KB
ID:	1551902

        Comment


        • #5
          Thanks to everyone for their helpful suggestions -- problem solved!

          Comment

          Working...
          X