搜索
人大经济论坛 附件下载

附件下载

所在主题:
文件名:  Graphics(2)CodeArchive.do
资料下载链接地址: https://bbs.pinggu.org/a-1892197.html
附件大小:

Stata 12 Graphics

Dawn Koffman

Office ofPopulation Research

Princeton University

坐标轴标签、置信区间、修改图例:

可以通过添加lfitci 和qfitci选项来对线性/二次回归曲线添加95%的置信区间

  1. sysuse lifeexp.dta, clear
  2. #d;
  3. twoway
  4. (lfitci lexp safewater if region == 2) /* North America */
  5. (scatter lexp safewater if region == 2)
  6. ,title("Life expectancy at birth by access to safe water, 1998")
  7. ytitle("Life expectancy at birth")
  8. xtitle("Percent of population with access to safe water")
  9. legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"));
  10. #d cr
复制代码

由于绘图时图例一般比较占据空间,所以应减少使用:一般替换图例的三种方法:
1)使用对比强烈的颜色,并某处简述属性:
  1. sysuse uslifeexp, clear
  2. twoway line le_male le_female year, lc(blue pink) legend(off)///
  3. ytitle("life expectancy") yla(, ang(h)) text(79.2 1980 "female", ///
  4. color(pink)) text(71 1976 "male", color(blue)) xtitle("") ///
  5. saving(g21, asis replace)
复制代码



2)在绘图区配以少数文字:
  1. u pollutants, clear
  2. local spec clw(medium) msize(*0.8)

  3. #delimit;
  4. twoway conn depth Au, `spec' ms(Oh) cmissing(n) ||
  5. conn depth Cd, `spec' ms(Th)|| conn depth As,
  6. `spec' ms(Sh)||conn depth Pb, `spec' ms(O)
  7. ||conn depth Cu, `spec' ms(T) || conn depth Zn, `spec' ms(S)
  8. ysc(reverse r(46 .))xsc(log) xsc(titlegap(*10)) yla(50(10)90, ang(h))
  9. xla(0.1 0.3 1 3 10 30 100)ytitle(Depth (cm)) xtitle(Concentration (ppm))
  10. text(48 `=Au[1]' "Au" 48 `=Cd[1]' "Cd" 48 `=As[1]' "As" 48 `=Pb[1]' "Pb"
  11. 48 `=Cu[1]' "Cu"48 `=Zn[1]' "Zn") legend(off) saving(g20, asis replace) ;
  12. #delimit cr
复制代码

3)使用by( ) legend(at)精确控制图例位置:

  1. set scheme s1color
  2. u cumbrian_cirques, clear
  3. net install gr42_6, from(http://www.stata-journal.com/software/sj12-1)
  4. qplotlength width, by(grade, legend(at(6) pos(0))) legend(col(1)///
  5. region(lcolor(none)) size(*1.3) rowgap(*3)) ysc(log) ///
  6. yla(200 500 1000 2000, ang(h)) saving(g22, asis replace)
复制代码

灰度Gray Scale:使用Gray Scale的两个主要优势:
1)一些杂志期刊社不接收彩色图像。
2)图中标记排列的更为清晰
此处推荐使用s1color
  1. set scheme s1color
  2. u cumbrian_cirques, clear
  3. separate length, by(grade) veryshortlabel

  4. scatter length? width, xsc(log) ysc(log) ms(O ..) ///
  5. mcolor(gs1 gs4 gs7 gs10 gs13) mlcolor(black ..)///
  6. msize(*1.5 ..) yti("`: variable label length'")yla(200 500 1000 2000) ///
  7. xla(200 500 1000 2000)legend(pos(11) ring(0) col(1)) ///
  8. saving(g4, asis replace)
复制代码


变量值标签及副标题修改

  1. #delimit ;
  2. twoway
  3. (lfitci lexp safewater if region == 2) /* North America */
  4. (scatter lexp safewater if region == 2, mlabel(country))
  5. ,title("Life expectancy at birth by access to safe water, 1998")
  6. subtitle("North America")
  7. ytitle("Life expectancy at birth")
  8. xtitle("Percent of population with access to safe water")
  9. legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"));
  10. #delimit cr
复制代码


移动标记位置1

  1. generate pos = 12 if country == "Panama"
  2. replace pos = 12 if country == "Honduras"
  3. replace pos = 10 if country == "Cuba"
  4. replace pos = 9 if country == "Jamaica"
  5. replace pos = 9 if country == "El Salvador"
  6. replace pos = 9 if country == "Trinidad and Tobago"
  7. replace pos = 9 if country == "Dominican Republic"
  8. #delimit ;
  9. twoway
  10. (lfitci lexp safewater if region == 2) /* North America */
  11. (scatter lexp safewater if region == 2
  12. , mlabel(country) mlabvposition(pos))
  13. ,title("Life expectancy at birth by access to safe water, 1998")
  14. subtitle("North America")
  15. ytitle("Life expectancy at birth")
  16. xtitle("Percent of population with access to safe water")
  17. legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"))
  18. plotregion(margin(r+10));
  19. #delimit cr
复制代码


移动标记位置2
  1. #delimit ;
  2. twoway
  3. (scatter lexp safewater if region == 2 | region == 3
  4. ,mlabel(country))
  5. ,title("Life expectancy at birth by access to safe water, 1998")
  6. subtitle("North and South America")
  7. ytitle("Life expectancy at birth")
  8. xtitle("Percent of population with access to safe water")
  9. plotregion(margin(r+10));
  10. #delimit cr
复制代码



异端标签位置和图例修改
  1. replace pos = 9 if country == "Argentina"
  2. replace pos = 9 if country == "Canada"
  3. replace pos = 9 if country == "Cuba"
  4. replace pos = 9 if country == "Panama"
  5. replace pos = 9 if country == "Venezuela"
  6. replace pos = 9 if country == "Jamaica"
  7. replace pos = 9 if country == "Dominican Republic"
  8. replace pos = 9 if country == "Ecuador"
  9. replace pos = 9 if country == "El Salvador"
  10. replace pos = 12 if country == "Puerto Rico"
  11. #delimit ;
  12. twoway
  13. (scatter lexp safewater if region == 2
  14. ,mlabel(country) mlabvposition(pos))
  15. (scatter lexp safewater if region == 3
  16. ,mlabel(country) mlabvposition(pos))
  17. ,title("Life expectancy at birth by access to safe water, 1998")
  18. subtitle("North and South America")
  19. ytitle("Life expectancy at birth")
  20. xtitle("Percent of population with access to safe water")
  21. legend(ring(0) pos(5) order(1 "North America" 2 "South America") cols(1));
  22. #delimit cr
复制代码


标记大孝符号、线条颜色
  1. #delimit ;
  2. twoway
  3. (scatter lexp safewater if region == 2
  4. ,mlabel(country) mlabvposition(pos) msize(small))
  5. (scatter lexp safewater if region == 3
  6. ,mlabel(country) mlabvposition(pos) msize(small) msymbol(circle_hollow))
  7. (lfit lexp safewater if region == 2, clcolor(navy))
  8. (lfit lexp safewater if region == 3, clcolor(maroon))
  9. ,title("Life expectancy at birth by access to safe water, 1998")
  10. subtitle("North and South America")
  11. ytitle("Life expectancy at birth")
  12. xtitle("Percent of population with access to safe water")
  13. legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America"
  14. 3 "North America linear fit" 4 "South America linear fit"));
  15. #delimit cr
复制代码


标记及标签、线型
  1. #delimit ;
  2. twoway
  3. (scatter lexp safewater if region == 2
  4. ,mlabel(country) mlabvposition(pos) msize(small))
  5. (scatter lexp safewater if region == 3
  6. ,mlabel(country) mlabvposition(pos) msize(small) msymbol(circle_hollow))
  7. (lfit lexp safewater if region == 2, clcolor(navy))
  8. (lfit lexp safewater if region == 3, clcolor(maroon))
  9. ,title("Life expectancy at birth by access to safe water, 1998")
  10. subtitle("North and South America")
  11. ytitle("Life expectancy at birth")
  12. xtitle("Percent of population with access to safe water")
  13. legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America"
  14. 3 "North America linear fit" 4 "South America linear fit"));
  15. #delimit cr
复制代码

分组画图:为数据集中的每一子集分别作图
  1. #delimit ;
  2. twoway scatter lexp safewater, by(region, total)
  3. ,ytitle("Life expectancy at birth")
  4. xtitle("Percent of population with access to safe
  5. water");
  6. #delimit cr
复制代码



注意分组画图选项:

style() title(" ")note(" ")
  1. #delimit ;
  2. twoway scatter lexp safewater
  3. ,by(region,total style(compact)
  4. title("Life expectancy by access to safe water") note(""))
  5. ytitle("Life expectancy at birth")
  6. xtitle("Percent of population with access to safe water");
  7. #delimit cr
复制代码


坐标轴选项:
结构、刻度
1. Y轴标签横向显示较佳即 yla(, ang(h)) 或者yla(, ang(0))
  1. #delimit ;
  2. twoway scatter lexp safewater
  3. , by(region,total style(compact)
  4. title("Life expectancy by access to safe water") note(""))
  5. xscale(range(20 100))
  6. xtick(20(10)100)
  7. xlabel(30(10)100, labsize(small))
  8. xtitle("Percent of population with access to safe water")
  9. ytitle("Life expectancy at birth")
  10. ylabel(55(5)80, angle(0));
  11. #delimit cr
复制代码

2. 可以使标签角度向左或者右微调,以调整轴与标签间距。(, ang(.001)) (左) or (, ang(-.001)) (右)

  1. u cumbrian_cirques, clear

  2. dotplot maxgrad, over(grade) name(g62,replace) ms(Oh) yla(, ang(0) format(%4.0g)) ///
  3. xla(,ang(-0.05) tlcolor(none)) subtitle("ang(-0.05)", color(black))
  4. dotplot maxgrad, over(grade) name(g63, replace) ms(Oh) yla(, ang(h) format(%4.0g)) ///
  5. xla(, tlcolor(none)) subtitle("ang(0)", color(black))
  6. graph combine g62 g63,col(1)
复制代码

3.突出强调某一坐标值的方法:?la(, custom add) //?代表x y
  1. sysuse citytemp, clear
  2. scatter tempjan tempjuly, yla(14 50 68 86, ang(h)) ///
  3. yla(32, custom add labsize(*1.4) ang(h)) xla(59(9)95) ///
  4. ms(Oh) subtitle({char 176}F, place(w) size(*1.4)) ///
  5. yli(32, lw(*.6) lc(gs12)) saving(g7, asis replace)


  6. //gs12 可输入第三方命令hue查看
复制代码



在上例中yla(32, custom add labsize(*1.4) ang(h))



图像的保存和命名保存图像于内存:
在图像命名时,使用name( )可在内存中保留多个图,同时也便于graph combine之后作为纵向/横向排列对比.
  1. #delimit ;
  2. twoway
  3. (scatter lexp safewater if region == 2,
  4. mcolor(black) msize(small)
  5. mlabel(country) mlabvposition(pos) mlabcolor(black))
  6. (lfit lexp safewater if region == 2, clcolor(black))
  7. ,name(north_america, replace)
  8. subtitle("North America", color(black))
  9. ylabel(,angle(0))
  10. ytitle("Life expectancy at birth")
  11. xtitle("Percent of population with access to safe water")
  12. legend(off);
  13. #delimit cr
复制代码

  1. #delimit ;
  2. twoway
  3. (scatter lexp safewater if region == 3,
  4. mcolor(black) msize(small)
  5. mlabel(country) mlabvposition(pos) mlabcolor(black))
  6. (lfit lexp safewater if region == 3, clcolor(black))
  7. ,name(south_america, replace)
  8. subtitle("South America", color(black))
  9. ylabel(, angle(0))
  10. ytitle("Life expectancy at birth")
  11. xtitle("Percent of population with access to safe water")
  12. legend(off);
  13. #delimit cr
复制代码

合并图像1

  1. #delimit ;
  2. graph combine north_america south_america
  3. ,title("Life expectancy by access to safe water", color(black)) col(1);
  4. #delimit cr
复制代码

合并图像2:
  1. #delimit ;
  2. graph combine north_america south_america
  3. ,title("Life expectancy by access to safe water", color(black)) col(1) xcommon ycommon xsize(7) ysize(10.5);
  4. #delimit cr
复制代码


保留于硬盘

  1. graph export north_america.wmf,replace
  2. graph export north_america.png,replace
复制代码


/*讲义加载及打开*/

  1. copy "http://opr.princeton.edu/workshops/201301/ws201301.pdf" "ws201301.pdf"

  2. !=start ws201301.pdf//Windows
  3. //!=open ws201301.pdf// Mac OS
复制代码


















    熟悉论坛请点击新手指南
下载说明
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。
2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。
3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。
(如有侵权,欢迎举报)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

GMT+8, 2026-1-7 18:26