- /*1.对数坐标散点图*/
- sysuse lifeexp, clear
- gen gnp000 = gnppc/1000
- label var gnp000 "GNP per capita, thousands of dollars"
- scatter lexp gnp000, xsca(log) ///
- xlabel(.5 2.5 10(10)40, grid)
复制代码
- /*2.泡泡图加权散点图*/
- sysuse census, clear
- gen drate = divorce / pop18p
- label var drate "Divorce rate"
- scatter drate medage [w=pop18p] if state!="Nevada", msymbol(Oh) ///
- note("State data excluding Nevada" ///
- "Area of symbol proportional to state's population aged 18+")
复制代码
- /*3.带置信区间的直线回归图*/
- sysuse auto, clear
- quietly regress mpg weight
- predict hat
- predict stf, stdf
- gen lo = hat - 1.96*stf
- gen hi = hat + 1.96*stf
- scatter mpg weight || line hat lo hi weight, pstyle(p2 p3 p3) sort
复制代码
- /*4.期望寿命折线图*/
- sysuse uslifeexp, clear
- gen diff = le_wm - le_bm
- label var diff "Difference"
- #delimit ;
- line le_wm year, yaxis(1 2) xaxis(1 2) ||
- line le_bm year || line diff year ||
- lfit diff year ||, ylabel(0(5)20, axis(2) gmin angle(horizontal))
- ylabel(0 20(10)80, gmax angle(horizontal))
- ytitle("", axis(2)) xlabel(1918, axis(2)) xtitle("", axis(2))
- ytitle("Life expectancy at birth (years)") title("White and black life expectancy")
- subtitle("USA, 1900-1999")
- note("Source: National Vital Statistics, Vol 50, No. 6" "(1918 dip caused by 1918 Influenza Pandemic)")
- legend(label(1 "White males") label(2 "Black males")) legend(col(1) pos(3)) ;
- #delimit cr
复制代码
- /*5.带置信区间的散点图*/
- sysuse auto, clear
- quietly regress mpg weight
- predict hat
- predict s, stdf
- gen low = hat - 1.96*s
- gen hi = hat + 1.96*s
- #delimit ;
- twoway rarea low hi weight,
- sort bcolor(gs14) || scatter mpg weight ;
- #delimit cr
复制代码
- /*6.折线、散点复合图*/
- sysuse sp500, clear
- #delimit ;
- twoway line close date,
- yaxis(1) || bar change date, yaxis(2) || in 1/52,
- ysca(axis(1) r(1000 1400)) ylab(1200(50)1400,
- axis(1)) ysca(axis(2) r(-50 300)) ylab(-50 0 50, axis(2))
- ytick(-50(25)50, axis(2) grid) legend(off)
- title("S&P 500") subtitle("January - March 2001")
- note("Source: Yahoo!Finance and Commodity Systems, Inc.")
- yline(1150, axis(1) lstyle(foreground)) ;
- #delimit cr
复制代码
- /*7.人口金字塔*/
- sysuse pop2000, clear
- replace maletotal = -maletotal/1e+6
- replace femtotal = femtotal/1e+6
- gen zero = 0
- #delimit ;
- twoway bar maletotal agegrp, horizontal xvarlab(Males) ||
- bar femtotal agegrp, horizontal xvarlab(Females) ||
- sc agegrp zero , mlabel(agegrp) mlabcolor(black) msymbol(i) || ,
- xtitle("Population in millions") ytitle("") plotregion(style(none))
- ysca(noline) ylabel(none) xsca(noline titlegap(-3.5))
- xlabel(-12 "12" -10 "10" -8 "8" -6 "6" -4 "4" 4(2)12 ,
- tlength(0) grid gmin gmax) legend(label(1 Males) label(2 Females))
- legend(order(1 2))
- title("US Male and Female Population by Age, 2000")
- note("Source: U.S. Census Bureau, Census 2000, Tables 1, 2 and 3") ;
- scheme(s1mono)
- #delimit cr
复制代码
- /*8.折线穗式图*/
- sysuse sp500, clear
- #delimit ;
- twoway line close date,
- yaxis(1) || spike change date, yaxis(2) ||
- , yscale(axis(1) r(700 1400))
- ylabel(1000(100)1400, axis(1))
- yscale(axis(2) r(-50 300))
- ylabel(-50 0 50, axis(2))
- ytick(-50(25)50, axis(2) grid)
- legend(off)
- title("S&P 500")
- subtitle("January - December 2001")
- note("Source: Yahoo!Finance and Commodity Systems, Inc.")
- yline(950, axis(1) lstyle(foreground)) ;
- #delimit cr
复制代码
- /*9合并图像*/
- set scheme s1color
- u cumbrian_cirques, clear
- scatter width length , name(g1_1, replace)
- scatter width length , ms(Oh) name(g1_2, replace)
- graph combine g1_1 g1_2, saving(g1, asis replace)
复制代码
- /*10.抖动对比*/
- set scheme s1color
- u cumbrian_cirques, clear
- scatter maxgrad grade, ms(Oh) yla(40(10)80, format(%4.0g)) xla(, valuelabel) xsc(r(0.8 5.2)) name(g2_1, replace)
- scatter maxgrad grade, ms(Oh) yla(40(10)80, format(%4.0g)) xla(, valuelabel) xsc(r(0.8 5.2)) jitter(1) name(g2_2, replace)
- graph combine g2_1 g2_2, saving(g2, replace asis)
复制代码
- /*11.点图*/
- set scheme s1color
- u cumbrian_cirques, clear
- dotplot maxgrad, over(grade) saving(g3, replace asis) ms(Oh) yla(, format(%4.0g))
复制代码
- /*12.散点图*/
- set scheme s1color
- u cumbrian_cirques, clear
- separate length, by(grade) veryshortlabel
- scatter length? width, xsc(log) ysc(log) ms(O ..) mcolor(gs1 gs4 gs7 gs10 gs13) ///
复制代码
- /*13.循环作图*/
- set scheme s1color
- u cumbrian_cirques, clear
-
- forval i = 1/5 {
- scatter length width if grade != `i', xsc(log) ysc(log) xla(200 500 1000 2000) ///
- yla(200 500 1000 2000) ms(oh) mc(blue*0.2) || scatter length width if grade ///
- == `i', xsc(log) ysc(log) xla(200 500 1000 2000) yla(200 500 1000 2000) ///
- ms(O) mc(blue) yla(, ang(h)) subtitle("`: label (grade) `i''", place(w)) ///
- legend(off) name(g11_`i', replace)
- }
-
- scatter length width, xsc(log) ysc(log) xla(200 500 1000 2000) yla(200 500 1000 2000) ///
- ms(O) mc(blue) yla(, ang(h)) name(g11_6, replace) subtitle("all", place(w))
-
- graph combine g11_1 g11_2 g11_3 g11_4 g11_5 g11_6, saving(g11, asis replace) imargin(small)
复制代码
|