📄 gr_example2.ado
字号:
title("Average Hourly Wage, 1988, Women Aged 34-46")
subtitle("by College Graduation, Marital Status, and SMSA residence")
note(
"Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics"
)
;
#delimit cr
Msg restore
end
program grbar5b0
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
Xeq graph bar wage, over(smsa) over(married) over(collgrad)
Msg restore
end
program grbar5b
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph bar wage, over(smsa, descend gap(-30))
over(married)
over(collgrad, relabel(1 "Not college graduate" 2 "College graduate"))
ytitle("")
title("Average Hourly Wage, 1988, Women Aged 34-46")
subtitle("by College Graduation, Marital Status, and SMSA residence")
note(
"Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics"
)
;
#delimit cr
Msg restore
end
program grbar7a
Msg preserve
preserve
Xeq sysuse educ99gdp, clear
Xeq gen total = private + public
Xeq graph hbar (asis) public private, over(country)
Msg restore
end
program grbar7
Msg preserve
preserve
Xeq sysuse educ99gdp, clear
Xeq gen total = private + public
#delimit ;
Xeq graph hbar (asis) public private,
over(country, sort(total) descending) stack
title("Spending on tertiary education as % of GDP, 1999",
span pos(11))
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span)
;
#delimit cr
Msg restore
end
program grbar8
Msg preserve
preserve
Xeq sysuse educ99gdp, clear
gen frac = private / (private + public)
#delimit ;
Xeq graph hbar (asis) public private,
over(country, sort(frac) descending) stack percentage
title("Public and private spending on tertiary education, 1999",
span pos(11))
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span)
;
#delimit cr
Msg restore
end
program grbartall
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph hbar wage, over(ind, sort(1)) over(collgrad)
title("Average hourly wage, 1988, women aged 34-46", span)
subtitle(" ")
note("Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics", span)
ysize(7)
;
#delimit cr
Msg restore
end
program grblab1
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph hbar wage,
over( occ, axis(off) sort(1) )
blabel( group, pos(base) color(bg) )
ytitle( "" )
by( union,
title("Average Hourly Wage, 1988, Women Aged 34-46")
note("Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics")
)
;
#delimit cr
Msg restore
end
program grdottall
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph dot wage, over(occ, sort(1))
ytitle("")
title("Average hourly wage, 1988, women aged 34-46", span)
subtitle(" ")
note("Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics", span)
;
#delimit cr
Msg restore
end
program grdottall2
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph dot (p10) wage (p90) wage,
over(occ, sort(2))
legend(label(1 "10th percentile") label(2 "90th percentile"))
title("10th and 90th percentiles of hourly wage", span)
subtitle("Women aged 34-46, 1988" " ", span)
note("Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics", span)
;
#delimit cr
Msg restore
end
program grdotby0
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
Xeq graph dot wage, over(occ) by(collgrad)
Msg restore
end
program grdotby
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph dot wage,
over(occ, sort(1))
by(collgrad,
title("Average hourly wage, 1988, women aged 34-46", span)
subtitle(" ")
note("Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics", span)
)
;
#delimit cr
Msg restore
end
program grbox1a
Msg preserve
preserve
Xeq sysuse bplong, clear
Xeq graph box bp, over(when) over(sex)
#delimit cr
Msg restore
end
program grbox1
Msg preserve
preserve
Xeq sysuse bplong, clear
#delimit ;
Xeq graph box bp, over(when) over(sex)
ytitle("Systolic blood pressure")
title("Response to treatment, by Sex")
subtitle("(120 Preoperative Patients)" " ")
note("Source: Fictional Drug Trial, StataCorp, 2003")
;
#delimit cr
Msg restore
end
program grbox2
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph hbox wage, over(ind, sort(1)) nooutside
ytitle("")
title("Hourly wage, 1988, woman aged 34-46", span)
subtitle(" ")
note(
"Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics"
, span)
;
#delimit cr
Msg restore
end
program grboxby
NotSmall
Msg preserve
preserve
Xeq sysuse nlsw88, clear
#delimit ;
Xeq graph hbox wage, over(ind, sort(1)) nooutside
ytitle("")
by(union,
title("Hourly wage, 1988, woman aged 34-46", span)
subtitle(" ")
note(
"Source: 1988 data from NLS, U.S. Dept. of Labor, Bureau of Labor Statistics"
, span)
)
;
#delimit cr
Msg restore
end
program grlinee
Msg preserve
preserve
Xeq sysuse uslifeexp2, clear
#delimit ;
Xeq line le year, sort
title("Line plot")
subtitle("Life expectancy at birth, U.S.")
note("1")
caption(
"Source: National Vital Statistics Report, Vol. 50 No. 6")
;
#delimit cr
Msg restore
end
program grscae
Msg preserve
preserve
Xeq sysuse uslifeexp2, clear
#delimit ;
Xeq scatter le year,
title("Scatter plot")
subtitle("Life expectancy at birth, U.S.")
note("1")
caption(
"Source: National Vital Statistics Report, Vol. 50 No. 6")
scheme(economist)
;
#delimit cr
Msg restore
end
program pie1a
Msg preserve
preserve
quietly {
drop _all
set obs 1
gen sales = 12
gen marketing = 14
gen research = 2
gen development = 8
label var sales "Sales"
label var marketing "Marketing"
label var research "Research"
label var development "Development"
}
Xeq graph pie sales marketing research development
Msg restore
end
program pie1
Msg preserve
preserve
quietly {
drop _all
set obs 1
gen sales = 12
gen marketing = 14
gen research = 2
gen development = 8
label var sales "Sales"
label var marketing "Marketing"
label var research "Research"
label var development "Development"
}
#delimit ;
Xeq graph pie sales marketing research development,
plabel(_all name, size(*1.5) color(white))
legend(off)
plotregion(lstyle(none))
title("Expenditures, XYZ Corp.")
subtitle("2002")
note("Source: 2002 Financial Report (fictional data)")
;
#delimit cr
Msg restore
end
program pie2
Msg preserve
preserve
quietly {
drop _all
set obs 16
gen qtr = 1 in 1/4
replace qtr = 2 in 5/8
replace qtr = 3 in 9/12
replace qtr = 4 in 13/16
gen str division = "Development" if mod(_n,4)==1
replace division = "Marketing" if mod(_n,4)==2
replace division = "Research" if mod(_n,4)==3
replace division = "Sales" if mod(_n,4)==0
gen cost = 1 in 1
replace cost = 4.5 in 2
replace cost = .3 in 3
replace cost = 3 in 4
replace cost = 2 in 5
replace cost = 3.0 in 6
replace cost = .5 in 7
replace cost = 4 in 8
replace cost = 2 in 9
replace cost = 4.0 in 10
replace cost = .6 in 11
replace cost = 3 in 12
replace cost = 3 in 13
replace cost = 2.5 in 14
replace cost = .6 in 15
replace cost = 2 in 16
}
#delimit ;
Xeq graph pie cost, over(division)
plabel(_all name, size(*1.5) color(white))
legend(off)
plotregion(lstyle(none))
title("Expenditures, XYZ Corp.")
subtitle("2002")
note("Source: 2002 Financial Report (fictional data)")
;
#delimit cr
Msg restore
end
program pie3
Msg preserve
preserve
quietly {
drop _all
set obs 2
gen year = 2002 in 1
gen sales = 12 in 1
gen marketing = 14 in 1
gen research = 2 in 1
gen development = 8 in 1
replace year = 2003 in 2
replace sales = 15 in 2
replace marketing = 17.5 in 2
replace research = 8.5 in 2
replace development = 10 in 2
label var sales "Sales"
label var marketing "Marketing"
label var research "Research"
label var development "Development"
}
#delimit ;
Xeq graph pie sales marketing research development,
plabel(_all name, size(*1.5) color(white))
by(year,
legend(off)
title("Expenditures, XYZ Corp.")
note("Source: 2002 Financial Report (fictional data)")
)
;
#delimit cr
Msg restore
end
program twopcspike1
Msg preserve
preserve
Xeq sysuse nlswide1, clear
Xeq twoway pcspike wage68 ttl_exp68 wage88 ttl_exp88 || ///
scatter wage68 ttl_exp68, msym(O) || ///
scatter wage88 ttl_exp88, msym(O) pstyle(p4) ///
mlabel(occ) xscale(range(17)) ///
title("Change in US Women's Experience and Earnings") ///
subtitle("By Occupation -- 1968 to 1988") ///
ytitle(Hourly wages) xtitle(Total experience) ///
note("Source: National Longitudinal Survey of Young Women") ///
legend(order(2 "1968" 3 "1988"))
Msg restore
end
program twopcspike2
Msg preserve
preserve
Xeq sysuse network1a, clear
Xeq twoway pcspike y_c x_c y_l x_l, pstyle(p3) || ///
pcspike y_c x_c y_r x_r, pstyle(p4) || ///
scatter y_l x_l , pstyle(p3) msize(vlarge) msym(O) ///
mlabel(lab_l) mlabpos(9) || ///
scatter y_c x_c , pstyle(p5) msize(vlarge) msym(O) || ///
scatter y_r x_r , pstyle(p4) msize(vlarge) ms(O) ///
mlabel(lab_r) mlabpos(3) ///
yscale(off) xscale(off) ylabels(, nogrid) legend(off) ///
plotregion(margin(30 15 3 3))
Msg restore
end
program barbsize
Msg preserve
preserve
Xeq drop _all
Xeq label drop _all
Xeq set obs 3
Xeq gen x1 = 1
Xeq gen x2 = 2
Xeq gen y1 = _n
Xeq gen y2 = _n
Xeq gen l = _n
Xeq label define blbl 1 "barbsize(0)" 2 "barbsize(2)" 3 "barbsize(4)"
Xeq label val l blbl
Xeq twoway pcarrow y1 x1 y2 x2 in 1, msize(4) mlabel(l) mlabs(4) ///
headlab || ///
pcarrow y1 x1 y2 x2 in 2, msize(4) mlabel(l) mlabs(4) ///
headlab barbsize(2) psty(p1) || ///
pcarrow y1 x1 y2 x2 in 3, msize(4) mlabel(l) mlabs(4) ///
headlab barbsize(4) psty(p1) ///
plotregion(margin(10 30 5 5)) ///
legend(off) ylabel(, nogrid) ///
yscale(range(.3 3.5) reverse off) xscale(off) ///
title("Example barbsize()s with msize(4)")
Msg restore
end
program pcarrow1
Msg preserve
preserve
_arrow_dta1
Xeq twoway pcarrow y1 x1 y2 x2
Msg restore
end
program pcarrow1b
Msg preserve
preserve
_arrow_dta1
Xeq twoway pcarrow y1 x1 y2 x2 , aspect(1) mlabel(time) ///
mlabvposition(pos) headlabel plotregion(margin(vlarge))
Msg restore
end
program _arrow_dta1
drop _all
set obs 2
qui gen x1 = 0
qui gen y1 = 0
qui gen x2 = 1 in 1
qui gen y2 = 0 in 1
qui replace x2 = 0 in 2
qui replace y2 = 1 in 2
qui gen str time = "3 o'clock" in 1
qui replace time = "12 o'clock" in 2
qui gen pos = 3 in 1
qui replace pos = 12 in 2
end
program pcarrow2
Msg preserve
preserve
Xeq sysuse nlsw88, clear
Xeq keep if occupation <= 8
Xeq collapse (p05) p05=wage (p95) p95=wage (p50) p50=wage, by(occupation)
Xeq gen mid = (p05 + p95) / 2
Xeq gen dif = (p95 - p05)
Xeq gsort -dif
Xeq gen srt = _n
Xeq twoway pcbarrow srt p05 srt p95 || ///
scatter srt mid, msymbol(i) mlabel(occupation) ///
mlabpos(12) mlabcolor(black) ///
plotregion(margin(t=8)) yscale(off) ///
ylabel(, nogrid) legend(off) ///
title("90 Percentile Range of US Women's Wages by Occupation") ///
ytitle(Hourly wages) ///
note("Source: National Longitudinal Survey of Young Women")
Msg restore
end
program pcarrowi1
Msg preserve
preserve
Xeq sysuse auto, clear
Xeq twoway qfitci mpg weight, stdf || ///
scatter mpg weight, ms(O) || ///
pcarrowi 41 2200 41 2060 (3) "VW Diesel" ///
31 3460 28 3280 (3) "Plymouth Arrow" ///
35 2250 35 2070 (3) "Datsun 210 and Subaru", ///
legend(order(1 2 3))
Msg restore
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -