楼主: 匿名
28902 14

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

匿名网友
楼主
匿名网友  发表于 2015-10-14 13:09:50 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

Stata 12/13 Graphics

Dawn Koffman

Office of  Population Research

Princeton University

/* 一个简单的例子*/
  1. sysuse uslifeexp.dta, clear
  2. graph twoway line le year
  3. twoway line le year
  4. line le year
复制代码
/*避免绘图窗口覆盖_grss*/
  1. ssc install grss
  2. sysuse uslifeexp.dta, clear
  3. grss graph twoway line le year
  4. grss twoway line le year
  5. grss line le year
复制代码

1.JPG


即在每次绘图语句前加grss
/*绘图格式*/
默认绘图方案为:s2color,查看可选:
  1. graph query, schemes
复制代码
设置绘图方案为:
  1. set scheme  绘图方案名
复制代码
或者在绘图选项后添加
  1. ,scheme(绘图方案名)
复制代码
  1. sysuse auto
  2. scatter mpg weight,title("默认 SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
  3. // s1color
  4. set scheme s1color
  5. scatter mpg weight,title("s1color SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
  6. //s1mono
  7. set scheme s1mono
  8. scatter mpg weight,title("s1mono SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
  9. //lean1
  10. * findit  lean1
  11. set scheme lean1
  12. scatter mpg weight,title("lean1 SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
  13. //lean2
  14. *findit  lean2
  15. set scheme lean2
  16. scatter mpg weight,title("lean2 SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
  17. //返回默认风格
  18. set scheme s2color
  19. scatter mpg weight,title("DEFAULT Scheme") subtitle("s2color") xlabel(1500(500)5000) ///
  20. ylabel(10(10)50) msymbol(o)
复制代码

15.jpg

  1. sysuse uslifeexp.dta, clear
  2. line le year, scheme(economist)                     //经济学人
复制代码

eco.png

注:Stata Journal为 :
  1. set scheme sj
复制代码
//永久设定自己偏好风格
  1. set scheme lean1, permanently //简写perma也可
复制代码
/*绘图区:三部分组成:1.plot选项(xlabel等)2.graph选项(title等)3.实际图像(graph)*/

绘图区.JPG


/*绘图语法*/

14.jpg

  1. /*
  2.                 Graph_Choice            Description
  3.                 -----------------------------------------------------------------------------
  4.                 graph twoway            scatterplots, line plots, etc.
  5.                 graph matrix            scatterplot matrices
  6.                 graph bar               bar charts
  7.                 graph dot               dot charts
  8.                 graph box               box-and-whisker plots
  9.                 graph pie               pie charts
  10.                 other                   more commands to draw statistical graphs
  11.                 -----------------------------------------------------------------------------
  12. */
复制代码

注:上述为通用语句,但是首词graph并不是一定要有的.

  1. sysuse auto
  2. graph twoway (scatter mpg weight, msymbol(d)), title("Scatterplot of MPG by Weight")
复制代码
关于符号尺寸、颜色的选项较多,在实际作图时可以使用showmarkers、huel第三方命令来帮助完成
/*自定义符号尺寸、颜色*/
  1. ssc install hue
  2. hue
复制代码

hue.png

  1. findit showmarkers
  2. showmarkers,over(msize)
  3. showmarkers,over(mcolor)
复制代码
或:
  1. palette symbolpalette
复制代码

Graph.png

mcolor.png

/*多变量同时作图:*/
  1. sysuse uslifeexp.dta, clear
  2. line le_wmale le_wfemale le_bmale le_bfemale year
复制代码

12.png

/ *添加文字:*/
text选项以估计坐标大致位置,bf加粗 it:斜体
  1. line le_wmale le_wfemale le_bmale le_bfemale year , ///
  2. text(32 1920 "{bf:1918} {it:Influenza} Pandemic", place(3))
复制代码

13.png

  1. sysuse uslifeexp, clear

  2. #delimit ;

  3. twoway line  le year || fpfit le year ||,
  4.   ytitle("Life Expectancy, years")
  5.   xlabel(1900 1918 1940(20)2000)
  6.   title("Life Expectancy at Birth")
  7.   subtitle("U.S., 1900-1999")
  8.   note("Source:  National Vital Statistics Report, Vol. 50 No. 6")
  9.   legend(off)
  10.   text(48.5 1923
  11.      "The 1918 Influenza Pandemic was the worst epidemic"
  12.      "known in the U.S."
  13.      "More citizens died than in all combat deaths of the"
  14.      "20th century.", box place(se) just(left) margin(l+4 t+1 b+1) width(85))
  15.   ;
  16. #delimit cr
复制代码

Graph.png

  1. /*

  2. margin边距:可以在left top bottom  right四个方向调节
  3. 句法:{l|r|b|t}[<space>][+|-|=]#
  4. 例如:
  5.                 l=5
  6.                 l=5 r=5
  7.                 l+5
  8.                 l+5 r=7.2  b-2 t+1

  9. margin(l+4 t+1 b+1)
  10. 距左4 上1 底1
  11. j:

  12.     justificationstyle    Description
  13.     ---------------------------------------------
  14.     left                  left-justified
  15.     center                centered
  16.     right                 right-justified
  17.     ---------------------------------------------
  18. place选项(图中为右下)

  19.         +--------------------------------------------------------+
  20.         |placement(nw)|       |placement(n)|       |placement(ne)|
  21.         |-------------+       +------------+       +-------------|
  22.         |                                                        |
  23.         |------------+        +------------+        +------------|
  24.         |placement(w)|        |placement(c)|        |placement(e)|
  25.         |------------+        +------------+        +------------|
  26.         |                                                        |
  27.         |-------------+       +------------+       +-------------|
  28.         |placement(sw)|       |placement(s)|       |placement(se)|
  29.         +--------------------------------------------------------+
  30. 更多参考help textbox_options
  31. */
复制代码

/*线性拟合*/
*实际绘图中lfit(拟合)经常与散点图等叠加在一起
  1. sysuse uslifeexp.dta, clear


  2. //法1
  3. scatter le year if year >= 1950 || lfit le year if year >= 1950

  4. //法2
  5. scatter ///
  6. le year if year >= 1950 ///
  7. || lfit le year if year >= 1950

  8. //法3
  9. twoway ///
  10. (scatter le year if year >= 1950) ///
  11. (lfit le year if year >= 1950)

  12. //法4
  13. #delimit ;
  14. twoway
  15. (scatter le year if year >= 1950)
  16. (lfit le year if year >= 1950);
  17. #delimit cr
复制代码

A1.png

"||"与"(  )" 含义相同,将不同图形区分开

将超长命令行分为若干短命令行:

1)使用"#delimit;"将默认分段符改为分号(可缩写为#d;只能在do文档中执行,执行时同时选中#d和#d cr之间的内容

2)使用///三斜杠将两行联系起来

3) //双斜杠一般代表注释,Stata会自动忽视//后面当前所在行的内容

  1. #delimit ;
  2. scatter le_male le_female year if year >= 1950
  3. || lfit le_male year if year >= 1950
  4. || lfit le_female year if year >= 1950;
  5. #delimit cr
复制代码

A3.png

双变量线性拟合图

  1. scatter le year if year >= 1925 ///
  2. || lfit le year if year >= 1925 & ///
  3. year < 1950 ///
  4. || lfit le year if year >= 1950
  5. /* OR */
  6. twoway ///
  7. (scatter le year if year >= 1925) ///
  8. (lfit le year if year >= 1925 & ///
  9. year < 1950) ///
  10. (lfit le year if year >= 1950)
  11. /* OR */
  12. #delimit ;
  13. scatter le year if year >= 1925
  14. || lfit le year if year >= 1925 & year < 1950
  15. || lfit le year if year >= 1950;
  16. #delimit cr
复制代码

A2.png


分阶段线性拟合图(对比Year=1950前后)

/* 添加标题、移除图例*/
legend(off)  title( ) text( )
  1. #delimit ;
  2. scatter le_male le_female year if year >= 1950
  3. || lfit le_male year if year >= 1950
  4. || lfit le_female year if year >= 1950
  5. ,title("US Male and Female Life Expectancy, 1950-2000")
  6. text(75 1978 "Female", place(3))
  7. text(68 1978 "Male", place(3))
  8. legend(off);
  9. #delimit cr
复制代码

B1.png

aaplot——在线性/二次拟合图中自动追加注释
这里注释主要添加方程、拟合优度、RMSE等,详见help aaplot
  1. ssc install aaplot
复制代码
  1. sysuse auto, clear
  2.     gen gpm = 1000 / mpg
  3.     label var gpm "Gallons per thousand miles"
  4.     aaplot gpm weight, name(g1)
  5.     aaplot gpm weight, lopts(lc(blue)) aformat(%04.3f) bformat(%06.5f) ///
  6.         rmseformat(%4.3f) name(g2)
  7.     aaplot gpm weight, quadratic qopts(lc(pink)) name(g3)
  8.     aaplot gpm weight, both name(g4)
  9.      aaplot gpm weight, both backdrop(lfitci gpm weight, color(gs12)) ///
  10.         name(g5)
复制代码
g1.png

g2.png


g3.png


g4.png

g5.png


如果只是想快速查看图像而不将结果输出,这时可以考虑使用graph7
  1. u auto,clear
  2. gr7 length price
复制代码

gr7.png


/*讲义下载及打开*/
  1. copy "http://opr.princeton.edu/workshops/201301/ws201301.pdf" "ws201301.pdf"
  2. !=start ws201301.pdf  //Windows
  3. //!=open ws201301.pdf  // Mac OS
复制代码

二维码

扫码加我 拉你入群

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

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

关键词:Stata基础 Stata 基础操作 tata scatterplot University

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

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

本帖被以下文库推荐

沙发
xddlovejiao1314 学生认证  发表于 2015-10-14 13:18:48
好贴,谢谢分享。
已有 1 人评分论坛币 热心指数 收起 理由
niuniuyiwan + 10 + 1 精彩帖子

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

藤椅
rrjj101022 发表于 2015-10-14 13:22:16
谢谢分享~~~
已有 2 人评分经验 论坛币 热心指数 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论
niuniuyiwan + 10 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 13  热心指数 + 1   查看全部评分

板凳
hua2007 发表于 2015-10-14 13:34:44
总结的很到位,赞一个!mark!
已有 2 人评分经验 论坛币 热心指数 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论
niuniuyiwan + 10 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 13  热心指数 + 1   查看全部评分

报纸
niuniuyiwan 在职认证  发表于 2015-10-15 10:34:22
hua2007 发表于 2015-10-14 13:34
总结的很到位,赞一个!mark!
谢谢,感谢支持!
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

地板
hua2007 发表于 2015-10-15 18:03:19
niuniuyiwan 发表于 2015-10-15 10:34
谢谢,感谢支持!
恩,版主给力呀!
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

7
lzt341 发表于 2015-10-16 10:18:57
要好好学习学习
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

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

8
Alfred_G 学生认证  发表于 2015-10-17 10:13:00
好资料,支持!
已有 1 人评分论坛币 热心指数 收起 理由
niuniuyiwan + 10 + 2 感谢支持

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

9
vivianhbs 发表于 2015-11-19 09:14:47
zhendehaobang

10
beijin2008 发表于 2016-5-8 22:48:58
好贴必须顶

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-5 20:11