# Stata绘图：绘制单个变量的时序图

Stata 连享会   主页 || 视频 || 推文

jiashuxu@ruc.edu.cn

Source: Chatfield, Mark D. "Graphing each individual's data over time." The Stata Journal 18.3 (2018): 503-516. [PDF]

Cox (2010) 在讨论数据图形化时，强调了个体之间或个体组之间的比较。我们在探索数据时，绘制每个个体随时间变化的数据（在单独的图表中）可能是一种容易被忽视的做法。从本质上讲，这种方法强调的是个体内部（而非个体之间）的差异。研究一些个体的时序图可以加深我们对数据的理解，并就可能对数据提出哪些研究问题、以及如何分析数据产生一些新的想法。随着时间的推移，数据中有很多关于个人的信息，绘制原始数据图可能是一项相当艰巨的挑战。各种符号和线段可以相互重叠，使我们制成的图形让人难以理解。对此，为单个个体绘制独立的时序图恰恰是一种解决办法。

## 1. 数据说明

``````. use http://www.stata-press.com/data/r15/nlswork.dta, clear
. xtset
. xtdescribe
. set scheme sj
``````

## 2. 绘制单变量的单个个体时序图

``````. generate  wage = exp(ln_wage)
. summarize wage, detail
. generate  wage_trunc = min(wage, 20) if !missing(wage)
. label variable wage_trunc "Wage (\$/hour)"
. keep if idcode <= 6
``````

``````putdocx clear
putdocx begin, landscape
putdocx paragraph
levelsof idcode, local(idcodes) clean
foreach id of local idcodes {
scatter wage_trunc year if idcode == `id' , connect(l) sort ///
ylabel(0 2 4 6 8 10 12 14 16 18 20 "≥20", angle(horizontal))  ///
yscale(range(0 20.5))  ///
xlabel(68 "1968" 73 "1973" 78 "1978" 83 "1983" 88 "1988")  ///
xmtick(68 69 70 71 72 73 75 77 78 80 82 83 85 87 88, grid notick)  ///
xscale(range(67.5 88.5))  ///
title("id `id'")  ///
graphregion(color(white))  ///
name(g`id', replace)
graph export "temp_graph.png", replace
putdocx image "temp_graph.png"
}
putdocx save "Example 1. Individual Graphs.docx", replace
erase "temp_graph.png"
``````

## 3. 绘制多变量的单个个体时序图

`````` use http://www.stata-press.com/data/r15/nlswork, clear
generate wage = exp(ln_wage)
keep idcode tenure hours union msp race grade age year wage
generate death_yr = 83 if idcode==5 // variable created for illustration
describe

. describe

Contains data from nlswork.dta
obs:        28,534              National Longitudinal Survey.  Young
Women 14-26 years of age in 1968
vars:            11              27 Nov 2016 08:14
size:       627,748
----------------------------------------------------------------------
storage   display    value
variable   type    format     label      variable label
----------------------------------------------------------------------
idcode      int     %8.0g                 NLS ID
year        byte    %8.0g                 interview year
age         byte    %8.0g                 age in current year
race        byte    %8.0g      racelbl    race
msp         byte    %8.0g                 1 if married, spouse present
grade       byte    %8.0g                 current grade completed
union       byte    %8.0g                 1 if union
tenure      float   %9.0g                 job tenure, in years
hours       int     %8.0g                 usual hours worked
wage        float   %9.0g
death_yr    float   %9.0g
----------------------------------------------------------------------
Sorted by: idcode  year
Note: Dataset has changed since last saved.
``````

``````.       distinct idcode
-------------------------------
|     total   distinct
--------+----------------------
idcode |     28534       4711
-------------------------------

.       distinct idcode grade, joint missing
----------------------------------
|     total   distinct
-----------+----------------------
(jointly) |     28534       4711
----------------------------------
``````

`````` summarize wage, detail
generate wage_trunc = min(wage, 20) if !missing(wage)
label variable wage_trunc "Wage (\$/hour)"

summarize tenure, detail
generate tenure_trunc = min(tenure, 20) if !missing(tenure)
label variable tenure_trunc "Tenure (years)"

summarize hours, detail
generate hours_trunc = min(hours, 60) if !missing(hours)
label var hours_trunc "Usual hours worked"
``````

`````` tabulate union, missing
generate lab_union = "U" if union==1
replace lab_union = "-" if union==0
tabulate msp, missing
generate lab_married = "M" if msp == 1
replace lab_married = "-" if msp == 0

decode race, generate(race_str)
keep if idcode <= 6
``````

``````generate y_union = 23
generate y_married = 26
generate y_age = 0

putdocx clear
putdocx begin, landscape
putdocx paragraph

levelsof idcode, local(idcodes) clean
foreach id of local idcodes {
local xline ""
local xlinetext ""
summarize death_yr if idcode == `id'
if r(N) > 0 {
local death_year = r(min)
if r(min) <= 88 {
local xline "xline(`death_year')"
local xlinetext `"text(21 `death_year' "Died")"'
}
}
levelsof race_str if idcode == `id', local(race_info) clean
levelsof grade if idcode == `id', local(grade_info)

twoway (scatter wage_trunc tenure_trunc year, connect(l l) sort ///
clwidth(thick) msymbol(O T)) ///
(scatter y_union y_married year, msymbol(i i) ///
mlabel(lab_union lab_married) mlabposition(0 0)) ///
(scatter y_age year, msymbol(i) mlabel(age) mlabposition(6) ///
mlabgap(*0.1) mlabcolor(gs8)) ///
(scatter hours_trunc year, yaxis(2) connect(l) sort msymbol(Sh) ///
clpattern(dash)) ///
if idcode == `id', ///
xlabel(68 "1968" 73 "1973" 78 "1978" 83 "1983" 88 "1988", notick ///
labsize(small) labgap(*6)) ///
xmtick(68 69 70 71 72 73 75 77 78 80 82 83 85 87 88, grid notick) ///
`xline' `xlinetext'   ///
xscale(range(67.5 88.5)) ///
plotregion(margin(zero)) ///
xtitle("") ///
text(-1 66 "Age", size(small) color(gs8)) ///
text(-2.7 66 "Year", size(small)) ///
graphregion(color(white)) ///
legend(order(1 - "             " 6 2) span cols(3)) ///
ytitle("Wage (\$/hour)   " "Tenure (years)    ") ///
ytitle("Usual hours worked        ", axis(2)) ///
title("id `id'") subtitle("(`race_info', grade `grade_info')") ///
ylabel(0 2 4 6 8 10 12 14 16 18 20 "≥20" 23 "Union?" 26 "Married?", ///
angle(horizontal)) ///
ylabel(0 6 12 18 24 30 36 42 48 54 60 "60+", axis(2) ///
angle(horizontal)) ///
yscale(range(0 27)) yscale(range(0 81) axis(2)) ///
name(g`id', replace)

graph export "graph.png", replace
putdocx image "graph.png"
}
putdocx save "Example 2. Individual Graphs.docx", replace
erase "graph.png"
``````

## 4. 参考文献

• Center for Human Resource Research. 1989. National Longitudinal Survey of Labor Market Experience, Young Women 14–26 years of age in 1968. Ohio State University.
• Cox, N. J. 2010. Speaking Stata: Graphing subsets. Stata Journal 10: 670–681. [PDF]
• Cox, N. J., and G. M. Longton. 2008. Speaking Stata: Distinct observations. Stata Journal 8: 557–568. [PDF]

## 相关课程

http://lianxh.duanshu.com

### 课程一览

Stata数据清洗 游万海 直播, 2 小时，已上线

Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

#### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。直播间 有很多视频课程，可以随时观看。
• 连享会-主页知乎专栏，300+ 推文，实证分析不再抓狂。
• 公众号推文分类： 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类，主流方法介绍一目了然：DID, RDD, IV, GMM, FE, Probit 等。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会学习群-常见问题解答汇总：
https://gitee.com/arlionn/WD

???? 连享会学习群-常见问题解答汇总：
???? https://gitee.com/arlionn/WD