楼主: niuniuyiwan
23064 20

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

  • 7关注
  • 91粉丝

VIP

学科带头人

89%

还不是VIP/贵宾

-

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

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

Stata 12 Graphics


Dawn Koffman

Office of  Population Research  

Princeton University


前言.PNG


输入数据

  1. clear
  2. input str14 country school1 school2 agemarriage1 agemarriage2
  3.         bangladesh       3.3      4.5      14.8         15.2
  4.         bolivia          6.9      7.6      19.8         20.1
  5.         colombia         7.9      8.6      20.3         20.3
  6.         dr               7.9      8.6      18.3         18.3
  7.         egypt            5.5      7.3      18.9         19.7
  8.         haiti            3.1      4.3      19.6         19.4
  9.         india            3.6      4.3      16.9         17.1
  10.         indonesia        5.9      7.5      18.1         19.3
  11.         morocco          1.6      2.7      18.7         19.6
  12.         nepal            2.4      3.6      16.9         17.4
  13.         pakistan         1.5      2.8      17.9         18.4
  14.         peru             8.1      8.8      20.2         20.6
  15. end
复制代码

/*标记趋势,分析样本点-pcarrow*/

  1. local sample MARRIED
  2. local x school
  3. local y agemarriage
  4. local note "Source: Two most recent DHS standard surveys, as of 5/2013"
  5. local xtitle "years of school"
  6. local ytitle "age at marriage"
  7. local ylabel ", angle(0)"
  8. gen pos = 3
  9. replace pos = 12 if country == "morocco"
  10. #delimit ;
  11. twoway pcarrow `y'1 `x'1 `y'2 `x'2,
  12. barbsize(1) lcolor(black) mcolor(black)
  13. || scatter `y'2 `x'2, mcolor(none)
  14. mlabel(country) mlabvposition(pos)
  15. || scatter `y'1 `x'1, msym(o)
  16. mcolor(black) msize(small)
  17. note("`note'", size(vsmall))
  18. ytitle("`ytitle'") xtitle("`xtitle'")
  19. ylabel(`ylabel')
  20. legend(off);
  21. #delimit cr
复制代码

Displaying Changes.png

例二:See SJ 5(2):282-284 (2005) and 9(4):621-639 (2009).

  1. u anorexia, replace
  2. bysort treatment (before after) : gen order1 = _n - _N/2
  3. twoway pcarrow before order1 after order1, pstyle(p1) || ///
  4. scatter before order1, pstyle(p1) ms(O) xla(none) xtitle("") yla(, ang(h)) ///
  5. ytitle("weight, lb") by(treatment, row(1) note("") legend(off)) ///
  6. saving(g12, asis replace)
复制代码

g12.png

数据: anorexia.rar (852 Bytes) 本附件包括:

  • anorexia.dta


直方图:

  1. set scheme s2mono
  2. sysuse nlsw88.dta, clear
  3. keep if age >=40 | age <= 44
  4. #delimit ;
  5. twoway histogram wage if wage <= 20, percent fcolor(gs12) lcolor(gs12) bin(30)
  6. title("Hourly Wage Distribution, Women 40-44")
  7. note("Source: Stata 12 NLSW 1988 extract", span)
  8. ylabel(, angle(0));
  9. #delimit cr
复制代码

Histogram.png

叠加直方图:

Overlaying Histograms.png

  1. #delimit ;
  2. twoway histogram wage if union == 1 & wage <= 20,
  3. percent fcolor(gs12) lcolor(gs12) bin(30) ||
  4. histogram wage if union == 0 & wage <= 20,
  5. percent fcolor(none) lcolor(black) bin(30)
  6. title("Hourly Wage Distribution by Union Status, Women 40-44")
  7. note("Source: Stata 12 NLSW 1988 extract", span) ylabel(, angle(0))
  8. legend(ring(0) pos(1) cols(1) order(1 "Union" 2 "Non-Union"));
  9. #delimit cr
复制代码

盒形图(box plot)

Boxplot.png

  1. #delimit ;
  2. graph box wage if age >= 40 & age <= 44, over(race)
  3. title("Hourly Wage by Race, Women 40-44 (n=918)")
  4. note("Source: Stata 12 NLSW 1988 extract")
  5. ylabel(, angle(0));
  6. #delimit cr
复制代码

分类别散点图

Scatter and Categorical Variable.png

  1. #delimit ;
  2. twoway scatter wage race if age >= 40 & age <= 44,
  3. title("Hourly Wage by Race, Women 40-44 (n=918)")
  4. note("Source: Stata 12 NLSW 1988 extract")
  5. xlabel(1 "white" 2 "black" 3 "other")
  6. xtitle("") xscale(range(0.5 3.5))
  7. ylabel(, angle(0));
  8. #delimit cr
复制代码

分类别散点图(抖动)

Scatter and Categorical Variable抖动.png

  1. #delimit ;
  2. twoway scatter wage race if age >= 40 & age <= 44,
  3. jitter(25) msize(tiny) mcolor(gs5)
  4. title("Hourly Wage by Race, Women 40-44 (n=918)")
  5. note("Source: Stata 12 NLSW 1988 extract")
  6. xlabel(1 "white" 2 "black" 3 "other", noticks)
  7. xtitle("") xscale(range(0.5 3.5)) ylabel(, angle(0));
  8. #delimit cr
复制代码

双向条形图

twoway rbar.png

  1. egen median = median(wage), by(race)
  2. egen upq = pctile(wage), p(75) by(race)
  3. egen loq = pctile(wage), p(25) by(race)
  4. egen iqr = iqr(wage), by(race)
  5. #delimit ;
  6. twoway rbar med upq race, barwidth(0.7) blc(black) bfc(none) lwidth(medthick) ||
  7. rbar med loq race, barwidth(0.7) blc(black) bfc(none) lwidth(medthick)
  8. title("Hourly Wage by Race, Women 40-44 (n=918)")
  9. note("Source: Stata 12 NLSW 1988 extract")
  10. xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
  11. xscale(range(0.5 3.5))
  12. yscale(range(0 42))
  13. ylabel(0 (10) 40, angle(0))
  14. ytitle("hourly wage")
  15. legend(off);
  16. #delimit cr
复制代码

盒形图(+散点)

Boxplot with Scatter.png

  1. *Boxplot with Whiskers and Scatter
  2. sysuse nlsw88.dta, clear
  3. set scheme s2mono
  4. egen median = median(wage), by(race)
  5. egen upq = pctile(wage), p(75) by(race)
  6. egen loq = pctile(wage), p(25) by(race)
  7. egen iqr = iqr(wage), by(race)
  8. egen upper = max(min(wage, upq + 1.5 * iqr)), by(race)
  9. egen lower = min(max(wage, loq - 1.5 * iqr)), by(race)
  10. #delimit ;
  11. twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
  12. rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
  13. rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
  14. rspike upq upper race, lwidth(medthick) ||
  15. rspike loq lower race, lwidth(medthick)
  16. title("Hourly Wage by Race, Women 40-44 (n=918)")
  17. note("Source: Stata 12 NLSW 1988 extract")
  18. xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
  19. xscale(range(0.5 3.5))
  20. yscale(range(0 42))
  21. ylabel(0 (10) 40, angle(0))
  22. ytitle("hourly wage")
  23. legend(off);
  24. #delimit cr
复制代码

Boxplot with Whiskers and Scatter.png

  1. egen median = median(wage), by(race)
  2. egen upq = pctile(wage), p(75) by(race)
  3. egen loq = pctile(wage), p(25) by(race)
  4. egen iqr = iqr(wage), by(race)
  5. #delimit ;
  6. twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
  7. rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick)||
  8. rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick)
  9. title("Hourly Wage by Race, Women 40-44 (n=918)")
  10. note("Source: Stata 12 NLSW 1988 extract")
  11. xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
  12. xscale(range(0.5 3.5))
  13. yscale(range(0 42))
  14. ylabel(0 (10) 40, angle(0))
  15. ytitle("hourly wage")
  16. legend(off);
  17. #delimit cr
复制代码

盒式-散点-虚线图-帽线

Boxplot with Whiskers,Caps and Scatter.png

  1. egen median = median(wage), by(race)
  2. egen upq = pctile(wage), p(75) by(race)
  3. egen loq = pctile(wage), p(25) by(race)
  4. egen iqr = iqr(wage), by(race)
  5. egen upper = max(min(wage, upq + 1.5 * iqr)), by(race)
  6. egen lower = min(max(wage, loq - 1.5 * iqr)), by(race)
  7. #delimit ;
  8. twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
  9. rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
  10. rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
  11. rspike upq upper race, lwidth(medthick) ||
  12. rspike loq lower race, lwidth(medthick)
  13. title("Hourly Wage by Race, Women 40-44 (n=918)")
  14. note("Source: Stata 12 NLSW 1988 extract")
  15. xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
  16. xscale(range(0.5 3.5))
  17. yscale(range(0 42))
  18. ylabel(0 (10) 40, angle(0))
  19. ytitle("hourly wage")
  20. legend(off);
  21. #delimit cr
复制代码

盒式-散点-虚线-帽线-均值图

Boxplot with Whiskers, Caps,Scatter and Means.png

  1. egen median = median(wage), by(race)
  2. egen upq = pctile(wage), p(75) by(race)
  3. egen loq = pctile(wage), p(25) by(race)
  4. egen iqr = iqr(wage), by(race)
  5. egen upper = max(min(wage, upq + 1.5 * iqr)), by(race)
  6. egen lower = min(max(wage, loq - 1.5 * iqr)), by(race)
  7. #delimit ;
  8. twoway scatter wage race, jitter(25) msize(tiny) mcolor(gs9) ||
  9. rbar med upq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
  10. rbar med loq race, barwidth(0.70) blc(black) bfc(none) lwidth(medthick) ||
  11. rcap loq lower race, lcolor(black) msize(*4) lwidth(medthick) ||
  12. rcap upq upper race, lcolor(black) msize(*4) lwidth(medthick)
  13. title("Hourly Wage by Race, Women 40-44 (n=918)")
  14. note("Source: Stata 12 NLSW 1988 extract")
  15. xlabel(1 "white" 2 "black" 3 "other", noticks) xtitle("")
  16. xscale(range(0.5 3.5))
  17. yscale(range(0 42))
  18. ylabel(0 (10) 40, angle(0))
  19. ytitle("hourly wage")
  20. legend(off);
  21. #delimit cr
复制代码

柱状图

Bar Graph.png

  1. #delimit ;
  2. graph bar (mean) wage,
  3. over(union) over(married) over(collgrad)
  4. blabel(bar, format(%9.2f)) yscale(off)
  5. title("1988 Mean Hourly Wage of Women Age 40-44")
  6. subtitle("by union status, marital status, and college graduation")
  7. note("Source: Stata 12 NLSW 1988 extract", span);
  8. #delimit cr
复制代码

水平条形图:

  1. #delimit ;
  2. graph hbar wage, over(ind, sort(1))
  3. over(collgrad)
  4. title("1988 Mean Hourly Wage of Women
  5. Age 40-44", span size(med))
  6. note("Source: Stata 12 NLSW 1988
  7. extract", span)
  8. nofill ytitle("") ysize(8);
  9. #delimit cr
复制代码

Horizontal Bar Graph.png

点图:

Dot Plot.png

  1. delimit ;
  2. graph dot wage, over(ind, sort(1))
  3. over(collgrad)
  4. title("1988 Mean Hourly Wage of Women
  5. Age 40-44", span size(med))
  6. note("Source: Stata 12 NLSW 1988
  7. extract", span)
  8. nofill ytitle("") ysize(8);
  9. #delimit cr
复制代码

双变量点图:

Dot Plot with 2 Variables.png

  1. #delimit ;
  2. graph dot (p25) wage (p75) wage,
  3. over(ind, sort(2)) over(collgrad)
  4. title("Upper and Lower Quartile of
  5. Hourly Wage", span)
  6. subtitle("Women Age 40-44, by College
  7. Graduation Status, 1988", span)
  8. note("Source: Stata 12 NLSW 1988
  9. extract", span)
  10. nofill ytitle("") ysize(8) xsize(6)
  11. legend(off);
  12. #delimit cr
复制代码
二维码

扫码加我 拉你入群

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

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

关键词:Stata基础 Stata 基础操作 tata Bangladesh University country india

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

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

本帖被以下文库推荐

沙发
xddlovejiao1314 学生认证  发表于 2015-7-13 10:51:57 |只看作者 |坛友微信交流群
好贴,谢谢分享,再接再厉。

使用道具

藤椅
ydb8848 发表于 2015-7-13 11:24:54 |只看作者 |坛友微信交流群
厉害,收下啦。。。呵呵
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

使用道具

板凳
niuniuyiwan 在职认证  发表于 2015-9-2 10:54:26 |只看作者 |坛友微信交流群
  1. sysuse lifeexp, clear
  2. gen gnp000 = gnppc/1000
  3. label var gnp000 "GNP per capita, thousands of dollars"
  4. scatter lexp gnp000, xsca(log) ///
  5. xlabel(.5 2.5 10(10)40, grid)
复制代码

Graph.png




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

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

使用道具

报纸
niuniuyiwan 在职认证  发表于 2015-9-17 10:58:35 |只看作者 |坛友微信交流群

1.PNG


2.PNG


Graph.png


jscatter.rar (831 Bytes) 本附件包括:

  • jscatter.do


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

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

使用道具

地板
玄一无相 在职认证  学生认证  发表于 2015-10-2 22:42:27 |只看作者 |坛友微信交流群
为什么这个箭头趋势图我实线不了呢,stata13.1
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

使用道具

7
tkt718 发表于 2015-10-2 23:37:30 来自手机 |只看作者 |坛友微信交流群
niuniuyiwan 发表于 2015-7-13 10:47
[/td][/tr]
[/table]

太神奇了!支持经管代码库……呵呵
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

使用道具

8
niuniuyiwan 在职认证  发表于 2015-10-3 09:37:08 |只看作者 |坛友微信交流群
tkt718 发表于 2015-10-2 23:37
太神奇了!支持经管代码库……呵呵
感谢支持
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

使用道具

9
niuniuyiwan 在职认证  发表于 2015-10-3 09:52:57 |只看作者 |坛友微信交流群
玄一无相 发表于 2015-10-2 22:42
为什么这个箭头趋势图我实线不了呢,stata13.1
您好,这里是帖子代码编辑错误,原帖已修正,同时感谢您的回复和支持。
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

使用道具

10
niuniuyiwan 在职认证  发表于 2015-10-15 11:41:57 |只看作者 |坛友微信交流群
  1. sysuse bplong, clear
  2. #delimit ;
  3. graph box bp,
  4.   over(when) over(sex)
  5.   ytitle("Systolic blood pressure")
  6.   title("Response to treatment, by Sex")
  7.   subtitle("(120 Preoperative Patients)" " ")
  8.   note("Source:  Fictional Drug Trial, Stata Corporation, 2003") ;
  9. #delimit cr
复制代码

Graph.png



使用道具

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

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

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

GMT+8, 2024-4-25 14:32