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
復(fù)制代碼 /*避免繪圖窗口覆蓋_grss*/ |
- ssc install grss
- sysuse uslifeexp.dta, clear
- grss graph twoway line le year
- grss twoway line le year
- grss line le year
復(fù)制代碼
即在每次繪圖語句前加grss
/*繪圖格式*/ |
默認(rèn)繪圖方案為:s2color,查看可選:設(shè)置繪圖方案為:或者在繪圖選項后添加- sysuse auto
- scatter mpg weight,title("默認(rèn) 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)
- //返回默認(rèn)風(fēng)格
- set scheme s2color
- scatter mpg weight,title("DEFAULT Scheme") subtitle("s2color") xlabel(1500(500)5000) ///
- ylabel(10(10)50) msymbol(o)
復(fù)制代碼
- sysuse uslifeexp.dta, clear
- line le year, scheme(economist) //經(jīng)濟(jì)學(xué)人
復(fù)制代碼
注:Stata Journal為 ://永久設(shè)定自己偏好風(fēng)格- set scheme lean1, permanently //簡寫perma也可
復(fù)制代碼 /*繪圖區(qū):三部分組成: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
- -----------------------------------------------------------------------------
- */
復(fù)制代碼注:上述為通用語句,但是首詞graph并不是一定要有的. - sysuse auto
- graph twoway (scatter mpg weight, msymbol(d)), title("Scatterplot of MPG by Weight")
復(fù)制代碼 關(guān)于符號尺寸、顏色的選項較多,在實際作圖時可以使用showmarkers、huel第三方命令來幫助完成
/*自定義符號尺寸、顏色*/ |
- findit showmarkers
- showmarkers,over(msize)
- showmarkers,over(mcolor)
復(fù)制代碼 或: |
|
|
/*多變量同時作圖:*/ |
- sysuse uslifeexp.dta, clear
- line le_wmale le_wfemale le_bmale le_bfemale year
復(fù)制代碼
|
/ *添加文字:*/
text選項以估計坐標(biāo)大致位置,bf加粗 it:斜體
- line le_wmale le_wfemale le_bmale le_bfemale year , ///
- text(32 1920 "{bf:1918} {it:Influenza} Pandemic", place(3))
復(fù)制代碼
- 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
復(fù)制代碼
- /*
- margin邊距:可以在left top bottom right四個方向調(diào)節(jié)
- 句法:{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
- */
復(fù)制代碼
|
/*線性擬合*/
*實際繪圖中l(wèi)fit(擬合)經(jīng)常與散點(diǎn)圖等疊加在一起 |
- 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
復(fù)制代碼
"||"與"( )" 含義相同,將不同圖形區(qū)分開 將超長命令行分為若干短命令行: 1)使用"#delimit;"將默認(rèn)分段符改為分號(可縮寫為#d;只能在do文檔中執(zhí)行,執(zhí)行時同時選中#d和#d cr之間的內(nèi)容 2)使用///三斜杠將兩行聯(lián)系起來 3) //雙斜杠一般代表注釋,Stata會自動忽視//后面當(dāng)前所在行的內(nèi)容 |
- #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
復(fù)制代碼
雙變量線性擬合圖 |
- 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
復(fù)制代碼
分階段線性擬合圖(對比Year=1950前后) |
/* 添加標(biāo)題、移除圖例*/
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
復(fù)制代碼
aaplot——在線性/二次擬合圖中自動追加注釋 這里注釋主要添加方程、擬合優(yōu)度、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)
復(fù)制代碼
如果只是想快速查看圖像而不將結(jié)果輸出,這時可以考慮使用graph7
- u auto,clear
- gr7 length price
復(fù)制代碼
|
/*講義下載及打開*/
|
- copy "http://opr.princeton.edu/workshops/201301/ws201301.pdf" "ws201301.pdf"
- !=start ws201301.pdf //Windows
- //!=open ws201301.pdf // Mac OS
復(fù)制代碼 |