Stata 12 Graphics |
Dawn Koffman Office of Population Research Princeton University |
|
输入数据,修改标签
- clear
- input str14 country tvhome birth5years idealnum age1stbirth school agemarriage
- bangladesh 33.9 .6 2.3 17.7 4.5 15.2
- bolivia 74.8 .8 2.6 20.3 7.6 20.1
- colombia 93.4 .4 2.4 20.7 8.6 20.3
- dr 83 .5 3.2 19.9 8.6 18.3
- egypt 95.9 .7 2.9 21.3 7.3 19.7
- haiti 27.9 .8 3.2 20.7 4.3 19.4
- india 47.9 .6 2.4 19.2 4.3 17.1
- indonesia 74 .5 2.8 20.7 7.5 19.3
- morocco 66.6 .7 3.3 21.4 2.7 19.6
- nepal 50.7 .6 2.2 19.6 3.6 17.4
- pakistan 57.8 .9 4.1 20.5 2.8 18.4
- peru 78.1 .6 2.5 21.1 8.8 20.6
- end
- label variable tvhome "TV at home (%)"
- label variable school "years of school"
- label variable birth5years "births in last 5 years"
- label variable idealnum "ideal number of children"
- label variable age1stbirth "age at first birth"
- label variable school "years of school"
- label variable agemarriage "age at first marriage"
矩阵关系图:
基本格式 graph matrix variable1 variable2 variable3, graph_options
Graph options:- /*
- half 展示一半,分析对比较易
- by(groupingvariable) 使用分组变量,注意先排序
- jitter(#) 大数据集用
- 技巧:msymbol(p) 数据集较大时 point
- msize(choice) 标记符号 [可选vtiny,tiny,small,medsmall,medium,medlarge,huge,vhuge,large,vlarge,ehuge]
- */
- #delimit ;
- local note "Source: Most recent DHS std survey: bangladesh bolivia colombia dr egypt
- haiti india indonesia morocco nepal pakistan peru, as of 5/2013";
- graph matrix school tvhome idealnum agemarriage age1stbirth birth5years,
- half note("`note'", size(vsmall));
- #delimit cr
关闭half,显示另一半
- #delimit ;
- local note "Source: Most recent DHS std survey: bangladesh bolivia colombia dr egypt
- haiti india indonesia morocco nepal pakistan peru, as of 5/2013";
- graph matrix school tvhome idealnum agemarriage age1stbirth birth5years,
- note("`note'", size(vsmall));
- #delimit cr
使用循环语句画图:
- #delimit ;
- local sample MARRIED;
- local note "Source: Most recent DHS standard survey, as of 5/2013"; foreach x in school tvhome {;
- foreach y in idealnum agemarriage age1stbirth birth5years {;
- twoway scatter `y' `x', mlabel(country) mlabsize(large) ylabel(, angle(0)) note("`note'") name("`x'_`y'", replace); graph export `x'_`y'_`sample'_mostrecent.emf, replace;
- };
- };
- #delimit cr
- //Combining many graphs on a page
- sysuse auto, clear
- graph drop _all
- drop make
- foreach i of varlist _all{
- capture confirm numeric variable `i' if _rc==0
- histogram `i', name(`i')
- local z "`z' `i'"
- graph combine `z'
- }
3.核密度图
- clear all
- sysuse auto
- kdensity length, bw(20)
- sysuse auto,clear
- kdensity price if foreign==0 , addplot( ///
- kdensity price if foreign==1 ) ///
- legend(label(1 "国内") label(2 "国外") col(2))
- sysuse auto,clear
- tw (kdensity price if foreign==0) ///
- (kdensity price if foreign==1) , ///
- legend(label(1 "国内") label(2 "国外") col(2))
面积图:注:只有在线图下的面积部分具有理论及分析意义时使用。
- sysuse auto, clear
- summarize price, mean
- local mean = r(mean)
- kdensity price, gen(x h)
- line h x, || area h x if x < `mean'
- sysuse gnp96, clear
- #delimit ;
- twoway area d.gnp96 date, xlabel(36(8)164, angle(90))
- ylabel(-100(50)200, angle(0))
- ytitle("Billions of 1996 Dollars")
- xtitle("")
- subtitle("Change in U.S. GNP", position(11))
- note("Source: U.S. Department of Commerce, Bureau of Economic Analysis")
- ;
- #delimit cr
//左右同时浮现且保持标题简洁的操作技巧:
//添加xtitle("")
- sysuse auto,clear
- sort foreign
- set scheme lean1
- histogram rep78, discrete by(foreign) percent ///
- addlabels xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5") ///
- gap(25) title("Repair Record in 1978") xtitle("")
在下端回避Repair Record in 1978