Stata 12 Graphics Dawn Koffman Office of Population Research Princeton University |
坐标轴标签、置信区间、修改图例:
可以通过添加lfitci 和qfitci选项来对线性/二次回归曲线添加95%的置信区间
- sysuse lifeexp.dta, clear
- #d;
- twoway
- (lfitci lexp safewater if region == 2) /* North America */
- (scatter lexp safewater if region == 2)
- ,title("Life expectancy at birth by access to safe water, 1998")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"));
- #d cr
1)使用对比强烈的颜色,并某处简述属性:
- sysuse uslifeexp, clear
- twoway line le_male le_female year, lc(blue pink) legend(off) ///
- ytitle("life expectancy") yla(, ang(h)) text(79.2 1980 "female", ///
- color(pink)) text(71 1976 "male", color(blue)) xtitle("") ///
- saving(g21, asis replace)
2)在绘图区配以少数文字:
- u pollutants, clear
- local spec clw(medium) msize(*0.8)
- #delimit;
- twoway conn depth Au, `spec' ms(Oh) cmissing(n) ||
- conn depth Cd, `spec' ms(Th) || conn depth As,
- `spec' ms(Sh) || conn depth Pb, `spec' ms(O)
- || conn depth Cu, `spec' ms(T) || conn depth Zn, `spec' ms(S)
- ysc(reverse r(46 .)) xsc(log) xsc(titlegap(*10)) yla(50(10)90, ang(h))
- xla(0.1 0.3 1 3 10 30 100) ytitle(Depth (cm)) xtitle(Concentration (ppm))
- text(48 `=Au[1]' "Au" 48 `=Cd[1]' "Cd" 48 `=As[1]' "As" 48 `=Pb[1]' "Pb"
- 48 `=Cu[1]' "Cu" 48 `=Zn[1]' "Zn") legend(off) saving(g20, asis replace) ;
- #delimit cr
3)使用by( ) legend(at)精确控制图例位置:
- set scheme s1color
- u cumbrian_cirques, clear
- net install gr42_6, from(http://www.stata-journal.com/software/sj12-1)
- qplot length width, by(grade, legend(at(6) pos(0))) legend(col(1) ///
- region(lcolor(none)) size(*1.3) rowgap(*3)) ysc(log) ///
- yla(200 500 1000 2000, ang(h)) saving(g22, asis replace)
1)一些杂志期刊社不接收彩色图像。
2)图中标记排列的更为清晰
此处推荐使用s1color
- 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) mlcolor(black ..) ///
- msize(*1.5 ..) yti("`: variable label length'") yla(200 500 1000 2000) ///
- xla(200 500 1000 2000) legend(pos(11) ring(0) col(1)) ///
- saving(g4, asis replace)
变量值标签及副标题修改
- #delimit ;
- twoway
- (lfitci lexp safewater if region == 2) /* North America */
- (scatter lexp safewater if region == 2, mlabel(country))
- ,title("Life expectancy at birth by access to safe water, 1998")
- subtitle("North America")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"));
- #delimit cr
移动标记位置1
- generate pos = 12 if country == "Panama"
- replace pos = 12 if country == "Honduras"
- replace pos = 10 if country == "Cuba"
- replace pos = 9 if country == "Jamaica"
- replace pos = 9 if country == "El Salvador"
- replace pos = 9 if country == "Trinidad and Tobago"
- replace pos = 9 if country == "Dominican Republic"
- #delimit ;
- twoway
- (lfitci lexp safewater if region == 2) /* North America */
- (scatter lexp safewater if region == 2
- , mlabel(country) mlabvposition(pos))
- ,title("Life expectancy at birth by access to safe water, 1998")
- subtitle("North America")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"))
- plotregion(margin(r+10));
- #delimit cr
移动标记位置2
- #delimit ;
- twoway
- (scatter lexp safewater if region == 2 | region == 3
- ,mlabel(country))
- ,title("Life expectancy at birth by access to safe water, 1998")
- subtitle("North and South America")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- plotregion(margin(r+10));
- #delimit cr
异端标签位置和图例修改
- replace pos = 9 if country == "Argentina"
- replace pos = 9 if country == "Canada"
- replace pos = 9 if country == "Cuba"
- replace pos = 9 if country == "Panama"
- replace pos = 9 if country == "Venezuela"
- replace pos = 9 if country == "Jamaica"
- replace pos = 9 if country == "Dominican Republic"
- replace pos = 9 if country == "Ecuador"
- replace pos = 9 if country == "El Salvador"
- replace pos = 12 if country == "Puerto Rico"
- #delimit ;
- twoway
- (scatter lexp safewater if region == 2
- ,mlabel(country) mlabvposition(pos))
- (scatter lexp safewater if region == 3
- ,mlabel(country) mlabvposition(pos))
- ,title("Life expectancy at birth by access to safe water, 1998")
- subtitle("North and South America")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(ring(0) pos(5) order(1 "North America" 2 "South America") cols(1));
- #delimit cr
标记大小、符号、线条颜色
- #delimit ;
- twoway
- (scatter lexp safewater if region == 2
- ,mlabel(country) mlabvposition(pos) msize(small))
- (scatter lexp safewater if region == 3
- ,mlabel(country) mlabvposition(pos) msize(small) msymbol(circle_hollow))
- (lfit lexp safewater if region == 2, clcolor(navy))
- (lfit lexp safewater if region == 3, clcolor(maroon))
- ,title("Life expectancy at birth by access to safe water, 1998")
- subtitle("North and South America")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America"
- 3 "North America linear fit" 4 "South America linear fit"));
- #delimit cr
标记及标签、线型
- #delimit ;
- twoway
- (scatter lexp safewater if region == 2
- ,mlabel(country) mlabvposition(pos) msize(small))
- (scatter lexp safewater if region == 3
- ,mlabel(country) mlabvposition(pos) msize(small) msymbol(circle_hollow))
- (lfit lexp safewater if region == 2, clcolor(navy))
- (lfit lexp safewater if region == 3, clcolor(maroon))
- ,title("Life expectancy at birth by access to safe water, 1998")
- subtitle("North and South America")
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America"
- 3 "North America linear fit" 4 "South America linear fit"));
- #delimit cr
- #delimit ;
- twoway scatter lexp safewater, by(region, total)
- ,ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe
- water");
- #delimit cr
注意分组画图选项:
style( ) title(" ") note(" ")- #delimit ;
- twoway scatter lexp safewater
- ,by(region,total style(compact)
- title("Life expectancy by access to safe water") note(""))
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water");
- #delimit cr
坐标轴选项:
结构、刻度
1. Y轴标签横向显示较佳即 yla(, ang(h)) 或者yla(, ang(0))
- #delimit ;
- twoway scatter lexp safewater
- , by(region,total style(compact)
- title("Life expectancy by access to safe water") note(""))
- xscale(range(20 100))
- xtick(20(10)100)
- xlabel(30(10)100, labsize(small))
- xtitle("Percent of population with access to safe water")
- ytitle("Life expectancy at birth")
- ylabel(55(5)80, angle(0));
- #delimit cr
2. 可以使标签角度向左或者右微调,以调整轴与标签间距。(, ang(.001)) (左) or (, ang(-.001)) (右)
- u cumbrian_cirques, clear
- dotplot maxgrad, over(grade) name(g62,replace) ms(Oh) yla(, ang(0) format(%4.0g)) ///
- xla(,ang(-0.05) tlcolor(none)) subtitle("ang(-0.05)", color(black))
- dotplot maxgrad, over(grade) name(g63, replace) ms(Oh) yla(, ang(h) format(%4.0g)) ///
- xla(, tlcolor(none)) subtitle("ang(0)", color(black))
- graph combine g62 g63,col(1)
- sysuse citytemp, clear
- scatter tempjan tempjuly, yla(14 50 68 86, ang(h)) ///
- yla(32, custom add labsize(*1.4) ang(h)) xla(59(9)95) ///
- ms(Oh) subtitle({char 176}F, place(w) size(*1.4)) ///
- yli(32, lw(*.6) lc(gs12)) saving(g7, asis replace)
- //gs12 可输入第三方命令hue查看
在上例中yla(32, custom add labsize(*1.4) ang(h))
图像的保存和命名保存图像于内存:
在图像命名时,使用name( )可在内存中保留多个图,同时也便于graph combine之后作为纵向/横向排列对比.
- #delimit ;
- twoway
- (scatter lexp safewater if region == 2,
- mcolor(black) msize(small)
- mlabel(country) mlabvposition(pos) mlabcolor(black))
- (lfit lexp safewater if region == 2, clcolor(black))
- ,name(north_america, replace)
- subtitle("North America", color(black))
- ylabel(,angle(0))
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(off);
- #delimit cr
- #delimit ;
- twoway
- (scatter lexp safewater if region == 3,
- mcolor(black) msize(small)
- mlabel(country) mlabvposition(pos) mlabcolor(black))
- (lfit lexp safewater if region == 3, clcolor(black))
- ,name(south_america, replace)
- subtitle("South America", color(black))
- ylabel(, angle(0))
- ytitle("Life expectancy at birth")
- xtitle("Percent of population with access to safe water")
- legend(off);
- #delimit cr
合并图像1
- #delimit ;
- graph combine north_america south_america
- ,title("Life expectancy by access to safe water", color(black)) col(1);
- #delimit cr
- #delimit ;
- graph combine north_america south_america
- ,title("Life expectancy by access to safe water", color(black)) col(1) xcommon ycommon xsize(7) ysize(10.5);
- #delimit cr
保留于硬盘
- graph export north_america.wmf,replace
- graph export north_america.png,replace
/*讲义加载及打开*/
- copy "http://opr.princeton.edu/workshops/201301/ws201301.pdf" "ws201301.pdf"
- !=start ws201301.pdf //Windows
- //!=open ws201301.pdf // Mac OS