Stata绘图技巧总结(二)

Stata绘图技巧总结(二)

本文是旧笔记的汇总,本文汇总了Stata绘图中的一些技巧。

人口金字塔图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
* 首先你需要安装一个获取数据集的命令
net install vgsg3, from("http://www.stata-press.com/data/vgsg3/"

* 首先我们准备好数据集
vguse pop2000mf, clear
gen female = femtotal/1000000
gen male = -male/1000000
gen zero = 0
* 然后就可以绘制这幅图了:
tw ///
bar male agegrp, horizontal || ///
bar female agegrp, horizontal || ///
sc agegrp zero, msymbol(i) mlabel(agegrp) ///
mlabcolor(black) xlabel(-12 "12" -8 "8" -4 "4" 4 8 12) ///
xtitle(人口数量(百万)) yscale(off) ylabel(, nogrid) ///
legend(order(1 "男性" 2 "女性") pos(6) row(1)) ///
note(数据来源:联合国人口司) text(18 10 "2000年")
graph export 人口金字塔图.png, replace

三组处理效果图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
clear
set obs 76
gen treatment = mod(_n,3)+1

gen w_before1 = rnormal(82, 4.8)
replace w_before1 = rnormal(81, 5.7) if treatment == 2
replace w_before1 = rnormal(83, 5) if treatment == 3

gen w_after1 = rnormal(85.6, 8.3)
replace w_after1 = rnormal(81.1, 4.7) if treatment == 2
replace w_after1 = rnormal(90, 5.4) if treatment == 3
//end of making up the data

label var w_before " Weight before treatment, lb"
label var w_after " Weight after treatment, lb"
label define treat ///
1 "Cognitive behavioural" ///
2 "Control" ///
3 "Family therapy"

label values treatment treat

gen byte one = 1
gen byte two = 2

tw ///
pcspike w_before one w_after two ///
if w_before <= w_after, ///
lcolor(gs12) || ///
pcspike w_before one w_after two ///
if w_before > w_after, ///
lcolor(gs2) lw(*1.2) xla(1 "Before" 2 "after") ///
xti("") yti("Weight, lb") ///
by(treatment, row(1) note("") leg(off) noixtick)
gr export 三组处理效果图.png, replace

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
clear
set obs 76
gen treatment = mod(_n,3)+1

gen w_before1 = rnormal(82, 4.8)
replace w_before1 = rnormal(81, 5.7) if treatment == 2
replace w_before1 = rnormal(83, 5) if treatment == 3

gen w_after1 = rnormal(85.6, 8.3)
replace w_after1 = rnormal(81.1, 4.7) if treatment == 2
replace w_after1 = rnormal(90, 5.4) if treatment == 3
//end of making up the data

label var w_before " Weight before treatment, lb"
label var w_after " Weight after treatment, lb"
label define treat ///
1 "Cognitive behavioural" ///
2 "Control" ///
3 "Family therapy"

label values treatment treat

sc w_after w_before w_before, ///
ms(Oh i) c(. l) lc(none gs6) ///
sort(w_before) yla(, ang(0)) ///
yti("Weight after treatment, lb") ///
by(treatment, row(1) note("Weight, lb") leg(off))

gr export 三组处理效果图3.png, replace

ztail命令

Stata
1
2
3
4
* 安装:
net install ztail.pkg, from("https://stats.idre.ucla.edu/stat/stata/ado/teach/")
ztail 0.05 1
gr export ztail.png, replace


Stata
1
2
ztail 0.10 2
gr export ztail2.png, replace

zmap

下面这种表示方法是调用变量标签:

Stata
1
2
. di "`: var label grade'"
current grade completed

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
webuse nlswork, clear
egen mean = mean(ln_wage), by(age grade)
egen tag = tag(age grade)
order age grade tag
gsort age grade
* tag()函数的作用是识别age和grade的奇异组(也就是相同组只有一组为1)
label var mean "mean ln wage"
su ln_wage if !missing(age, grade), detail
* ssc install zmap
zmap mean grade age if tag, ///
breaks(.993 1.166 1.361 1.641 1.964 2.275 2.456) ///
ms(S ..) ysc(on) xsc(on) legend(on pos(3) col(1)) ///
yla(0/18, ang(h)) ytitle(`: var label grade') ///
xla(15(5)45) note("") mc(blue)
gre zmap

Stata绘图中的配色

Stata
1
2
3
4
5
6
7
8
9
10
11
sysuse auto, clear
gr bar mpg, asyvars over(rep78) ///
bar(1, bcolor(red*0.4)) ///
bar(2, bcolor(red*0.2)) ///
bar(3, bcolor(blue*0.2)) ///
bar(4, bcolor(blue*0.4)) ///
bar(5, bcolor(blue*0.6)) ///
leg(row(1) title("REP78")) ///
title("Mean MPG for rep78") ///
yla(, nogrid angle(0)) scheme(s1mono)
graph export Stata绘图中的配色.png, replace

标签自动换行

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
sysuse auto, clear

label define rep78 1 "Very low rating in 1978" ///
2 "Mediocre - Low Rating in 1978" ///
3 "Medium Rating in 1978" ///
4 "Not bad rating in 1978, considering" ///
5 "Excellent in 1978--the best of the year!"

label values rep78 rep78

* ssc install splitvallabels
splitvallabels rep78 if rep78 > 3, length(11) recode
gr bar mpg if rep78 > 3, over(rep78, relabel(`r(relabel)'))

regaxis:帮助确定坐标轴的尺度、范围和刻度标签

Stata
1
2
3
4
5
6
7
* ssc install regaxis
sysuse auto, clear
regaxis rep78, cycle(0.5) singleok margin(0.5) ///
lrange(yrange) lticks(ylabs)

regaxis weight, include(0) lticks(xlabs)
sc rep78 weight, ysc(range(`yrange')) ylab(`ylabs') xlab(`xlab')

radar:绘制雷达图

Stata
1
2
3
4
5
6
7
8
* ssc install radar
sysuse auto, clear
radar make turn mpg trunk if for, ///
lc(red*0.6 blue*0.6 green*0.6) ///
lw(*1 *2 *3) r(0 12 14 18 50) ///
leg(pos(6) row(1)) note("") aspect(1)

gr export radar.png, replace

pcarrowi命令中的msize()和barbsize()的关系

Stata
1
2
3
4
5
6
7
8
9
10
11
12
clear all
set scheme vg_rose
local cmd
forval x = 1/7{
forval y = 0/`x'{
local x1 = `x' + 0.5
local cmd (pcarrowi `y' `x' `y' `x1', msize(`x') barbsize(`y')) `cmd'
}
}
di "`cmd'"
gr tw `cmd', yla(0(1)8, ang(0)) xla(1(1)8) leg(off) yti("barbsize()") xti("msize()") plotr(color(white))
gr export msize_barbsize.png, replace

muxyplot:多个y和多个x的散点图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ssc install muxyplot
/*
muxyplot produces a scatter or twoway plot for yvars versus xvars,
all of which xvars are measured on comparable scales. The number of
yvars must equal the number of xvars. For example,

muxyplot y1 y2 y3 x1 x2 x3

plots y1 versus x1, y2 versus x2 and y3 versus x3, all on the same
scatter plot.
*/
sysuse auto, clear
muxyplot price weight rep78 headroom
gre muxyplot

mm_plot:一个mata绘制Mata矩阵的命令

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* install
* ssc install moremata

mata:
for(i = 0; i < 2 * pi() * 1000; i ++ ){
a = (i / 1000) * cos(i / 1000)
a1 = (i / 1000) * sin(i / 1000)
if(i == 0){
za = a
za1 = a1
}
else{
za = za\a
za1 = za1\a
}
c = za, za1
}
c
mata: mm_plot(c)
end
gre mm_plot1

Stata
1
2
3
4
mata: a = (1,2,3,4\2,5,7,8\4,5,6,7)
mata: a
mata: mm_plot(a)
gre mm_plot2

这个绘图命令会以矩阵的最后一列为x变量,其余列为y变量绘制散点图。

missingplot:可视化缺失值

Stata
1
2
3
4
* ssc install missingplot
webuse nlsw88, clear
missingplot, var labels mlabcolor(blue*0.4 ..)
* 标签表示该缺失位置的行号

kdbox:箱线图+核密度估计图

Stata
1
2
3
* ssc install kdbox
sysuse auto, clear
kdbox mpg

Stata
1
kdbox mpg, normal mean

hmap:绘制热力图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
clear all
set obs 64
g n = int(uniform()*10)
g x = 1 + int((_n-1)/8)
g y = 1 + mod((_n-1),8)
la def xlab 1 "one" 2 "two" 7 "seven" 8 "eight"
la def ylab 3 "three" 4 "four" 5 "five" 6 "six"
la val x xlab
la val y ylab
ta y x [fw = n]
* ssc install hmap
hmap x y n

Stata
1
hmap x y n, sc mon

Stata
1
hmap x y n, sc

histbox:直方图+箱线图

Stata
1
2
3
sysuse auto, clear
* ssc install histbox
histbox mpg

Stata
1
histbox mpg, mean freq normal bin(10)

hist3:绘制不规则的条形图

Stata
1
2
3
4
* ssc install hist3
sysuse auto, clear
hist3 price
gr export hist3a.png, replace

Stata
1
2
hist3 price, v(3500(2000)9000,10000,18000)
gr export hist3b.png, replace

Stata
1
2
hist3 price, v(3500(1000)8000,10000,15000) xlab(3000(2000)16000)
gr export hist3c.png, replace

himatrix:网格散点图+高亮某些散点

  • himatrix draws a scatterplot matrix highlighting the observations selected by exp2.
Stata
1
2
sysuse auto, clear
himatrix price headroom trunk weight, hilite(foreign==1)

Stata
1
2
himatrix price headroom trunk weight, h(foreign==1) hsymbol(O)
gre himatrix1

Stata
1
himatrix price headroom trunk weight, h(_n==71) hs(VW Diesel) b2t(" ")

hangroot:悬挂图

Stata
1
2
3
4
5
6
7
* ssc install hangroot
sysuse nlsw88, clear
gen lnw = ln(wage)
reg lnw grade age ttl_exp tenure
predict resi, r
hangroot resi
gre hangroot

Stata
1
hangroot resi, bar

labmask命令:将某一变量生成为另一数值变量的赋值标签

Stata
1
2
3
4
5
6
7
8
9
sysuse auto, clear
sort price
gen order = _n
labmask order, val(make)
tw bar price order, horizontal ///
yla(8 44 71, val angle(0)) ///
yti("")

gr export labmask命令.png, replace

linkplot:双类别数据对比图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
* 双类别数据对比图
clear
input ///
A B id
13.2 14.0 1
8.2 8.8 2
10.9 11.2 3
14.3 14.2 4
10.7 11.8 5
6.6 6.4 6
9.5 9.8 7
10.8 11.3 8
8.8 9.3 9
13.3 13.6 10
end

ren A wearA
ren B wearB
reshape long wear, string i(id) j(j)
encode j, gen(material)
* ssc install linkplot
linkplot material wear, link(id) yla(1 2, val ang(0)) ///
ysc(r(0.2 2.5))
gre linkplot

full_palette:绘制颜色地图,并且会生成一个颜色值的数据集

Stata
1
2
3
* ssc install full_palette
full_palette
gre full_palette

另外一个绘制颜色地图的命令:

Stata
1
vgcolormap

flower命令:解决散点遮盖的一种思路

flower图使用向日葵的图案来解决重叠点的显示问题:

Stata
1
2
3
sysuse auto, clear
flower mpg displ
gr export flower1.png, replace

ybins()/xbins():指定分组数

Stata
1
2
flower mpg weight, yb(10) xb(10)
gr export flower2.png, replace

psize(size):花瓣大小:默认值为100

Stata
1
2
flower mpg weight, yb(10) xb(10) psize(200)
gr export flower3.png, replace

petals(maxnum):花瓣的最大数量,默认为10,不能小于2

Stata
1
2
flower mpg weight, yb(10) xb(10) psize(200) petals(4)
gr export flower4.png, replace

# Stata

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了767.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×