Scatter 1

Overlapping scattergraphs per group with multiple axes. This graph is useful for contrasting the gradient of the linear fit by suppressing the differential effect of the intercept. Special coding features include using consice value labels for use as marker labels, the automation of minima and maxima for multiple axes, the elimination of multiple multivariate outliers using Hadi's filter, the use of string locals for repeated options, and SMCL syntax in titles.

scatter1

Code

#delimit ;
sysuse auto, clear ;

label define origin1 0 "D" 1 "F" ;
label value foreign origin1 ;

generate ln_price = ln(price) ;
generate ln_mpg = ln(mpg) ;

foreach x of varlist ln_price ln_mpg { ;
      forvalues z = 0/1 { ;
            summarize `x' if foreign==`z' ;
            local l_`x'`z' = round(r(min),0.1) ;
            local u_`x'`z' = round(r(max),0.1) ;
      } ;
} ;

hadimvo ln_price ln_mpg if foreign, generate(f1) p(.05) ;
hadimvo ln_price ln_mpg if !foreign, generate(f0) p(.05) ;

local yx "ln_price ln_mpg" ;
local opt "msymbol(none) mlabel(foreign) msize(*.25) mlabposition(0)" ;
local ax1 "xaxis(1) yaxis(1)" ;
local ax2 "xaxis(2) yaxis(2)" ;

twoway (lfit `yx' if foreign, color(navy) xaxis(1) yaxis(1)) 
             (lfit `yx' if !foreign, color(red) xaxis(2) yaxis(2)) 
             (scatter `yx' if foreign, `opt' mlabcolor(navy) `ax1') 
             (scatter `yx' if !foreign, `opt' mlabcolor(red) `ax2') 
             (scatter `yx' if f1==1, `opt' mlabcolor(navy) mlabel(make) `ax1') 
             (scatter `yx' if f0==1, `opt' mlabcolor(red) mlabel(make) `ax2') 
             , legend(off)
             xlabel(`l_ln_mpg1'(.2)`u_ln_mpg1', axis(1))
             xlabel(`l_ln_mpg0'(.2)`u_ln_mpg0', axis(2))
             ylabel(`l_ln_price1'(.2)`u_ln_price1', axis(1) angle(0))
             ylabel(`l_ln_price0'(.2)`u_ln_price0', axis(2) angle(0)) 
             xtitle("{bf:Foreign} cars" "ln of mpg", axis(1) col(navy)) 
             xtitle("{bf:Domestic} cars" "ln of mpg", axis(2) col(red)) 
             ytitle("ln of price" "{bf:Foreign} cars", axis(1) col(navy) angle(90)) 
             ytitle("ln of price" "{bf:Domestic} cars", axis(2) col(red) angle(90)) 
             plotregion(fcolor(gs15)) graphregion(lcolor(gs0))
             saving(pricempg.gph, replace) ;

graph export pricempg.pdf, replace ;