Stata 12/13 Graphics |
Dawn Koffman Office of Population Research Princeton University |
/* 一个简单的例子*/
- sysuse uslifeexp.dta, clear
- graph twoway line le year
- twoway line le year
- line le year
复制代码 /*避免绘图窗口覆盖_grss*/ |
- ssc install grss
- sysuse uslifeexp.dta, clear
- grss graph twoway line le year
- grss twoway line le year
- grss line le year
复制代码
即在每次绘图语句前加grss
/*绘图格式*/ |
默认绘图方案为:s2color,查看可选:设置绘图方案为:或者在绘图选项后添加- sysuse auto
- scatter mpg weight,title("默认 SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
- // s1color
- set scheme s1color
- scatter mpg weight,title("s1color SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
- //s1mono
- set scheme s1mono
- scatter mpg weight,title("s1mono SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
- //lean1
- * findit lean1
- set scheme lean1
- scatter mpg weight,title("lean1 SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
- //lean2
- *findit lean2
- set scheme lean2
- scatter mpg weight,title("lean2 SCHEME") xlabel(1500(500)5000) ylabel(10(10)50) msymbol(o)
- //返回默认风格
- set scheme s2color
- scatter mpg weight,title("DEFAULT Scheme") subtitle("s2color") xlabel(1500(500)5000) ///
- ylabel(10(10)50) msymbol(o)
复制代码
- sysuse uslifeexp.dta, clear
- line le year, scheme(economist) //经济学人
复制代码
注:Stata Journal为 ://永久设定自己偏好风格- set scheme lean1, permanently //简写perma也可
复制代码 /*绘图区:三部分组成:1.plot选项(xlabel等)2.graph选项(title等)3.实际图像(graph)*/
|
/*绘图语法*/
- /*
- Graph_Choice Description
- -----------------------------------------------------------------------------
- graph twoway scatterplots, line plots, etc.
- graph matrix scatterplot matrices
- graph bar bar charts
- graph dot dot charts
- graph box box-and-whisker plots
- graph pie pie charts
- other more commands to draw statistical graphs
- -----------------------------------------------------------------------------
- */
复制代码注:上述为通用语句,但是首词graph并不是一定要有的. - sysuse auto
- graph twoway (scatter mpg weight, msymbol(d)), title("Scatterplot of MPG by Weight")
复制代码 关于符号尺寸、颜色的选项较多,在实际作图时可以使用showmarkers、huel第三方命令来帮助完成
/*自定义符号尺寸、颜色*/ |
- findit showmarkers
- showmarkers,over(msize)
- showmarkers,over(mcolor)
复制代码 或: |
|
|
| /*多变量同时作图:*/ |
- sysuse uslifeexp.dta, clear
- line le_wmale le_wfemale le_bmale le_bfemale year
复制代码
|
/ *添加文字:*/
text选项以估计坐标大致位置,bf加粗 it:斜体
- line le_wmale le_wfemale le_bmale le_bfemale year , ///
- text(32 1920 "{bf:1918} {it:Influenza} Pandemic", place(3))
复制代码
- sysuse uslifeexp, clear
- #delimit ;
- twoway line le year || fpfit le year ||,
- ytitle("Life Expectancy, years")
- xlabel(1900 1918 1940(20)2000)
- title("Life Expectancy at Birth")
- subtitle("U.S., 1900-1999")
- note("Source: National Vital Statistics Report, Vol. 50 No. 6")
- legend(off)
- text(48.5 1923
- "The 1918 Influenza Pandemic was the worst epidemic"
- "known in the U.S."
- "More citizens died than in all combat deaths of the"
- "20th century.", box place(se) just(left) margin(l+4 t+1 b+1) width(85))
- ;
- #delimit cr
复制代码
- /*
- margin边距:可以在left top bottom right四个方向调节
- 句法:{l|r|b|t}[<space>][+|-|=]#
- 例如:
- l=5
- l=5 r=5
- l+5
- l+5 r=7.2 b-2 t+1
- margin(l+4 t+1 b+1)
- 距左4 上1 底1
- j:
- justificationstyle Description
- ---------------------------------------------
- left left-justified
- center centered
- right right-justified
- ---------------------------------------------
- place选项(图中为右下)
- +--------------------------------------------------------+
- |placement(nw)| |placement(n)| |placement(ne)|
- |-------------+ +------------+ +-------------|
- | |
- |------------+ +------------+ +------------|
- |placement(w)| |placement(c)| |placement(e)|
- |------------+ +------------+ +------------|
- | |
- |-------------+ +------------+ +-------------|
- |placement(sw)| |placement(s)| |placement(se)|
- +--------------------------------------------------------+
- 更多参考help textbox_options
- */
复制代码
|
/*线性拟合*/
*实际绘图中lfit(拟合)经常与散点图等叠加在一起 |
- sysuse uslifeexp.dta, clear
- //法1
- scatter le year if year >= 1950 || lfit le year if year >= 1950
- //法2
- scatter ///
- le year if year >= 1950 ///
- || lfit le year if year >= 1950
- //法3
- twoway ///
- (scatter le year if year >= 1950) ///
- (lfit le year if year >= 1950)
- //法4
- #delimit ;
- twoway
- (scatter le year if year >= 1950)
- (lfit le year if year >= 1950);
- #delimit cr
复制代码
"||"与"( )" 含义相同,将不同图形区分开 将超长命令行分为若干短命令行: 1)使用"#delimit;"将默认分段符改为分号(可缩写为#d;只能在do文档中执行,执行时同时选中#d和#d cr之间的内容 2)使用///三斜杠将两行联系起来 3) //双斜杠一般代表注释,Stata会自动忽视//后面当前所在行的内容 |
- #delimit ;
- scatter le_male le_female year if year >= 1950
- || lfit le_male year if year >= 1950
- || lfit le_female year if year >= 1950;
- #delimit cr
复制代码
双变量线性拟合图 |
- scatter le year if year >= 1925 ///
- || lfit le year if year >= 1925 & ///
- year < 1950 ///
- || lfit le year if year >= 1950
- /* OR */
- twoway ///
- (scatter le year if year >= 1925) ///
- (lfit le year if year >= 1925 & ///
- year < 1950) ///
- (lfit le year if year >= 1950)
- /* OR */
- #delimit ;
- scatter le year if year >= 1925
- || lfit le year if year >= 1925 & year < 1950
- || lfit le year if year >= 1950;
- #delimit cr
复制代码
分阶段线性拟合图(对比Year=1950前后) |
/* 添加标题、移除图例*/
legend(off) title( ) text( ) |
- #delimit ;
- scatter le_male le_female year if year >= 1950
- || lfit le_male year if year >= 1950
- || lfit le_female year if year >= 1950
- ,title("US Male and Female Life Expectancy, 1950-2000")
- text(75 1978 "Female", place(3))
- text(68 1978 "Male", place(3))
- legend(off);
- #delimit cr
复制代码
aaplot——在线性/二次拟合图中自动追加注释 这里注释主要添加方程、拟合优度、RMSE等,详见help aaplot
- sysuse auto, clear
- gen gpm = 1000 / mpg
- label var gpm "Gallons per thousand miles"
- aaplot gpm weight, name(g1)
- aaplot gpm weight, lopts(lc(blue)) aformat(%04.3f) bformat(%06.5f) ///
- rmseformat(%4.3f) name(g2)
- aaplot gpm weight, quadratic qopts(lc(pink)) name(g3)
- aaplot gpm weight, both name(g4)
- aaplot gpm weight, both backdrop(lfitci gpm weight, color(gs12)) ///
- name(g5)
复制代码
如果只是想快速查看图像而不将结果输出,这时可以考虑使用graph7
- u auto,clear
- gr7 length price
复制代码
|
/*讲义下载及打开*/
|
- copy "http://opr.princeton.edu/workshops/201301/ws201301.pdf" "ws201301.pdf"
- !=start ws201301.pdf //Windows
- //!=open ws201301.pdf // Mac OS
复制代码 |