楼主: niuniuyiwan
10944 8

[Stata] Stata 基础操作:绘图概述(二) [推广有奖]

  • 7关注
  • 91粉丝

VIP

学科带头人

89%

还不是VIP/贵宾

-

威望
0
论坛币
12644 个
通用积分
1551.1038
学术水平
1116 点
热心指数
1164 点
信用等级
1058 点
经验
8347 点
帖子
1625
精华
1
在线时间
2447 小时
注册时间
2010-10-10
最后登录
2024-4-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Stata 12 Graphics

Dawn Koffman

Office of  Population 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.png

由于绘图时图例一般比较占据空间,所以应减少使用:一般替换图例的三种方法:
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)
复制代码

Graph.png



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
复制代码

g20.png

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. qplot  length 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)
复制代码

g22.png

灰度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)
复制代码

g4.png


变量值标签及副标题修改

  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
复制代码

2.png


移动标记位置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
复制代码

3.png


移动标记位置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
复制代码

4.png



异端标签位置和图例修改
  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
复制代码

5.png


标记大小、符号、线条颜色
  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
复制代码

6.png


标记及标签、线型
  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
复制代码

Graph1.png

分组画图:为数据集中的每一子集分别作图
  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
复制代码

7.png



注意分组画图选项:

                               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
复制代码

8.png


坐标轴选项:
结构、刻度
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
复制代码

9.png

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)
复制代码

Graph.png

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查看
复制代码

g12.png



在上例中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
复制代码

north_america.png

  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
复制代码

south_america.png

合并图像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
复制代码

12.png

合并图像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
复制代码

pic13.png


保留于硬盘

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

14.JPG

         Graphics(2)CodeArchive.do (5.44 KB)


/*讲义加载及打开*/

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

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
















二维码

扫码加我 拉你入群

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

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

关键词:Stata 基础操作 tata EXPECTANCY Population University 置信区间 access 坐标轴 title

已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 100 + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  论坛币 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

本帖被以下文库推荐

沙发
rrjj101022 发表于 2015-10-12 09:51:42 |只看作者 |坛友微信交流群
谢谢分享~~~
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

使用道具

藤椅
零点晓敏 学生认证  发表于 2015-10-12 10:10:35 来自手机 |只看作者 |坛友微信交流群
niuniuyiwan 发表于 2015-10-12 09:50
坐标轴标签、置信区间、修改图例标签及副标题修改
移动标记位置

学习了~多谢楼主!
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

使用道具

板凳
xddlovejiao1314 学生认证  发表于 2015-10-12 11:25:48 |只看作者 |坛友微信交流群
好贴,谢谢分享。
已有 1 人评分论坛币 热心指数 收起 理由
niuniuyiwan + 10 + 3 感谢师兄支持!

总评分: 论坛币 + 10  热心指数 + 3   查看全部评分

使用道具

报纸
岳西YH 发表于 2015-10-12 18:02:06 |只看作者 |坛友微信交流群
牛人啊,谢谢,迫切需要
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

使用道具

地板
niuniuyiwan 在职认证  发表于 2015-10-15 13:33:58 |只看作者 |坛友微信交流群
  1. sysuse uslifeexp, clear
  2. line le_male   year, saving(male)
  3. line le_female year, saving(female)
  4. graph combine male.gph female.gph, ycommon
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 100 + 100 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 100  论坛币 + 100  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

7
niuniuyiwan 在职认证  发表于 2015-10-15 13:34:59 |只看作者 |坛友微信交流群
  1. sysuse uslifeexp, clear
  2. line le_male   year, ylab(,grid) saving(male)
  3. line le_female year, ylab(,grid) saving(female)
  4. gr combine male.gph female.gph, col(1) scale(1)
复制代码

Graph.png



已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 100 + 100 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 100  论坛币 + 100  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

8
lzt341 发表于 2015-10-16 10:20:14 |只看作者 |坛友微信交流群
感谢楼主分享,期待下一期
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

使用道具

9
updatingpower 发表于 2018-5-15 11:41:53 |只看作者 |坛友微信交流群
请教楼主个问题,第一个图“lfitci 和qfitci选项来对线性/二次回归曲线添加95%的置信区间”,如何调整显著性水平,比如:调整到99%置信区间呢?谢谢!!

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 13:20