Stata 12 Graphics
|
輸入數(shù)據(jù)
- clear
- input str14 country school1 school2 agemarriage1 agemarriage2
- bangladesh 3.3 4.5 14.8 15.2
- bolivia 6.9 7.6 19.8 20.1
- colombia 7.9 8.6 20.3 20.3
- dr 7.9 8.6 18.3 18.3
- egypt 5.5 7.3 18.9 19.7
- haiti 3.1 4.3 19.6 19.4
- india 3.6 4.3 16.9 17.1
- indonesia 5.9 7.5 18.1 19.3
- morocco 1.6 2.7 18.7 19.6
- nepal 2.4 3.6 16.9 17.4
- pakistan 1.5 2.8 17.9 18.4
- peru 8.1 8.8 20.2 20.6
- end
/*標(biāo)記趨勢(shì),分析樣本點(diǎn)-pcarrow*/
- local sample MARRIED
- local x school
- local y agemarriage
- local note "Source: Two most recent DHS standard surveys, as of 5/2013"
- local xtitle "years of school"
- local ytitle "age at marriage"
- local ylabel ", angle(0)"
- gen pos = 3
- replace pos = 12 if country == "morocco"
- #delimit ;
- twoway pcarrow `y'1 `x'1 `y'2 `x'2,
- barbsize(1) lcolor(black) mcolor(black)
- || scatter `y'2 `x'2, mcolor(none)
- mlabel(country) mlabvposition(pos)
- || scatter `y'1 `x'1, msym(o)
- mcolor(black) msize(small)
- note("`note'", size(vsmall))
- ytitle("`ytitle'") xtitle("`xtitle'")
- ylabel(`ylabel')
- legend(off);
- #delimit cr
例二:See SJ 5(2):282-284 (2005) and 9(4):621-639 (2009).
- u anorexia, replace
- bysort treatment (before after) : gen order1 = _n - _N/2
- twoway pcarrow before order1 after order1, pstyle(p1) || ///
- scatter before order1, pstyle(p1) ms(O) xla(none) xtitle("") yla(, ang(h)) ///
- ytitle("weight, lb") by(treatment, row(1) note("") legend(off)) ///
- saving(g12, asis replace)
數(shù)據(jù):
直方圖:
- set scheme s2mono
- sysuse nlsw88.dta, clear
- keep if age >=40 | age <= 44
- #delimit ;
- twoway histogram wage if wage <= 20, percent fcolor(gs12) lcolor(gs12) bin(30)
- title("Hourly Wage Distribution, Women 40-44")
- note("Source: Stata 12 NLSW 1988 extract", span)
- ylabel(, angle(0));
- #delimit cr
疊加直方圖:
復(fù)制代碼
- #delimit ;
- twoway histogram wage if union == 1 & wage <= 20,
- percent fcolor(gs12) lcolor(gs12) bin(30) ||
- histogram wage if union == 0 & wage <= 20,
- percent fcolor(none) lcolor(black) bin(30)
- title("Hourly Wage Distribution by Union Status, Women 40-44")
- note("Source: Stata 12 NLSW 1988 extract", span) ylabel(, angle(0))
- legend(ring(0) pos(1) cols(1) order(1 "Union" 2 "Non-Union"));
- #delimit cr
盒形圖(box plot)
復(fù)制代碼
- #delimit ;
- graph box wage if age >= 40 & age <= 44, over(race)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- ylabel(, angle(0));
- #delimit cr
分類(lèi)別散點(diǎn)圖
復(fù)制代碼
- #delimit ;
- twoway scatter wage race if age >= 40 & age <= 44,
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other")
- xtitle("") xscale(range(0.5 3.5))
- ylabel(, angle(0));
- #delimit cr
分類(lèi)別散點(diǎn)圖(抖動(dòng))
- #delimit ;
- twoway scatter wage race if age >= 40 & age <= 44,
- jitter(25) msize(tiny) mcolor(gs5)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other", noticks)
- xtitle("") xscale(range(0.5 3.5)) ylabel(, angle(0));
- #delimit cr
雙向條形圖
復(fù)制代碼
- egen median = median(wage), by(race)
- egen upq = pctile(wage), p(75) by(race)
- egen loq = pctile(wage), p(25) by(race)
- egen iqr = iqr(wage), by(race)
- #delimit ;
- twoway rbar med upq race, barwidth(0.7) blc(black) bfc(none) lwidth(medthick) ||
- rbar med loq race, barwidth(0.7) blc(black) bfc(none) lwidth(medthick)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
- xscale(range(0.5 3.5))
- yscale(range(0 42))
- ylabel(0 (10) 40, angle(0))
- ytitle("hourly wage")
- legend(off);
- #delimit cr
盒形圖(+散點(diǎn))
- *Boxplot with Whiskers and Scatter
- sysuse nlsw88.dta, clear
- set scheme s2mono
- egen median = median(wage), by(race)
- egen upq = pctile(wage), p(75) by(race)
- egen loq = pctile(wage), p(25) by(race)
- egen iqr = iqr(wage), by(race)
- egen upper = max(min(wage, upq + 1.5 * iqr)), by(race)
- egen lower = min(max(wage, loq - 1.5 * iqr)), by(race)
- #delimit ;
- twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
- rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
- rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
- rspike upq upper race, lwidth(medthick) ||
- rspike loq lower race, lwidth(medthick)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
- xscale(range(0.5 3.5))
- yscale(range(0 42))
- ylabel(0 (10) 40, angle(0))
- ytitle("hourly wage")
- legend(off);
- #delimit cr
復(fù)制代碼
- egen median = median(wage), by(race)
- egen upq = pctile(wage), p(75) by(race)
- egen loq = pctile(wage), p(25) by(race)
- egen iqr = iqr(wage), by(race)
- #delimit ;
- twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
- rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick)||
- rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
- xscale(range(0.5 3.5))
- yscale(range(0 42))
- ylabel(0 (10) 40, angle(0))
- ytitle("hourly wage")
- legend(off);
- #delimit cr
盒式-散點(diǎn)-虛線圖-帽線
復(fù)制代碼
- egen median = median(wage), by(race)
- egen upq = pctile(wage), p(75) by(race)
- egen loq = pctile(wage), p(25) by(race)
- egen iqr = iqr(wage), by(race)
- egen upper = max(min(wage, upq + 1.5 * iqr)), by(race)
- egen lower = min(max(wage, loq - 1.5 * iqr)), by(race)
- #delimit ;
- twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
- rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
- rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
- rspike upq upper race, lwidth(medthick) ||
- rspike loq lower race, lwidth(medthick)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
- xscale(range(0.5 3.5))
- yscale(range(0 42))
- ylabel(0 (10) 40, angle(0))
- ytitle("hourly wage")
- legend(off);
- #delimit cr
盒式-散點(diǎn)-虛線-帽線-均值圖
復(fù)制代碼
- egen median = median(wage), by(race)
- egen upq = pctile(wage), p(75) by(race)
- egen loq = pctile(wage), p(25) by(race)
- egen iqr = iqr(wage), by(race)
- egen upper = max(min(wage, upq + 1.5 * iqr)), by(race)
- egen lower = min(max(wage, loq - 1.5 * iqr)), by(race)
- #delimit ;
- twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
- rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
- rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
- rcap loq lower race, lcolor(black) msize(*4) lwidth(medthick) ||
- rcap upq upper race, lcolor(black) msize(*4) lwidth(medthick)
- title("Hourly Wage by Race, Women 40-44 (n=918)")
- note("Source: Stata 12 NLSW 1988 extract")
- xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
- xscale(range(0.5 3.5))
- yscale(range(0 42))
- ylabel(0 (10) 40, angle(0))
- ytitle("hourly wage")
- legend(off);
- #delimit cr
柱狀圖
- #delimit ;
- graph bar (mean) wage,
- over(union) over(married) over(collgrad)
- blabel(bar, format(%9.2f)) yscale(off)
- title("1988 Mean Hourly Wage of Women Age 40-44")
- subtitle("by union status, marital status, and college graduation")
- note("Source: Stata 12 NLSW 1988 extract", span);
- #delimit cr
水平條形圖:
- #delimit ;
- graph hbar wage, over(ind, sort(1))
- over(collgrad)
- title("1988 Mean Hourly Wage of Women
- Age 40-44", span size(med))
- note("Source: Stata 12 NLSW 1988
- extract", span)
- nofill ytitle("") ysize(8);
- #delimit cr
點(diǎn)圖:
復(fù)制代碼
- delimit ;
- graph dot wage, over(ind, sort(1))
- over(collgrad)
- title("1988 Mean Hourly Wage of Women
- Age 40-44", span size(med))
- note("Source: Stata 12 NLSW 1988
- extract", span)
- nofill ytitle("") ysize(8);
- #delimit cr
雙變量點(diǎn)圖:
- #delimit ;
- graph dot (p25) wage (p75) wage,
- over(ind, sort(2)) over(collgrad)
- title("Upper and Lower Quartile of
- Hourly Wage", span)
- subtitle("Women Age 40-44, by College
- Graduation Status, 1988", span)
- note("Source: Stata 12 NLSW 1988
- extract", span)
- nofill ytitle("") ysize(8) xsize(6)
- legend(off);
- #delimit cr