Stata 绘图技巧总结(一)

Stata 绘图技巧总结(一)

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

百分比堆叠柱状图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sysuse auto, clear
gen a = _n
collapse a, by(for rep78)
drop if missing(for, rep78)
bysort for rep78: gen sum = sum(a)
replace a = 0 if a == .
levelsof rep78, local(aa)
gen csum = .

foreach i of local aa{
summ a if `i' == rep78
replace csum = a/`r(sum)' if `i' == rep78
}

gr bar (sum) csum, over(for) over(rep78) ///
asyvars stack leg(size(vsmall)) per blabel(bar, ///
pos(center) format(%3.1f) size(medsmall) color(black))

包含年月的坐标轴

方法一

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
36
37
38
39
40
41
42
clear all
inp str10 date ricepr
"01/01/2006" 700
"01/02/2006" 700
"01/03/2006" 900
"01/04/2006" 900
"01/05/2006" 900
"01/06/2006" 900
"01/07/2006" 900
"01/08/2006" 933.33
"01/09/2006" 1000
"01/10/2006" 1000
"01/11/2006" 1000
"01/12/2006" 1000
"01/01/2007" 1050
"01/02/2007" 1087.5
"01/03/2007" 1100
"01/04/2007" 1100
"01/05/2007" 1100
"01/06/2007" 1100
"01/07/2007" 1175
"01/08/2007" 1200
"01/09/2007" 1200
"01/10/2007" 1300
"01/11/2007" 1400
"01/12/2007" 1400
end

gen int year = yofd(date(date, "DMY"))
gen byte aux = _n

di c(Mons)
token `c(Mons)'

forval i = 1/24{
if `i' == 1 la def month `i' "`=year[`i']'``i''", modify
if inrange(`i', 2, 12) la def month `i' "``i''", modify
if `i' == 13 la def month `i' "`=year[`i-12']' ``=`i'-12''", modify
if `i' > 13 la def month `i' "``=`i'-12''", modify
}
la val aux month
tw li ricepr aux, xla(#24, labsize(small) ang(45) val) xti("") yla(, ang(0) nogrid) scheme(s1mono)

方法二

这里用到了 labmask 命令,安装方法:

Stata
1
net install gr0034.pkg, from("http://www.stata-journal.com/software/sj8-2/")

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
36
37
38
39
clear all
inp str10 date ricepr
"01/01/2006" 700
"01/02/2006" 700
"01/03/2006" 900
"01/04/2006" 900
"01/05/2006" 900
"01/06/2006" 900
"01/07/2006" 900
"01/08/2006" 933.33
"01/09/2006" 1000
"01/10/2006" 1000
"01/11/2006" 1000
"01/12/2006" 1000
"01/01/2007" 1050
"01/02/2007" 1087.5
"01/03/2007" 1100
"01/04/2007" 1100
"01/05/2007" 1100
"01/06/2007" 1100
"01/07/2007" 1175
"01/08/2007" 1200
"01/09/2007" 1200
"01/10/2007" 1300
"01/11/2007" 1400
"01/12/2007" 1400
end

gen date2 = date(date, "DMY")
gen monthlabel = substr("JanFebMarAprMayJunJulAugSepOctNovDec", month(date2)*3-2, 3)

replace monthlabel = monthlabel + " " + string(year(date2), "%5.0f") if month(date2) == 1
cap which labmask
if _rc != 0{
net install gr0034.pkg, from("http://www.stata-journal.com/software/sj8-2/")
}
labmask date2, values(monthlabel)
levelsof date2, local(levels)
line ricepr date2, xla(`levels', val ang(45) labsize(small)) xti(日期) yla(, ang(0) nogrid) scheme(s1mono)

成份条形图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
clear all
sysuse auto, clear
twoway__histogram_gen mpg if foreign, gen(freq_foreign x) freq start(0) width(2)
drop if missing(x)
keep freq_foreign x
save a1, replace
sysuse auto, clear
twoway__histogram_gen mpg if ~foreign, gen(freq_domestic x) freq start(0) width(2)
drop if missing(x)
keep freq_domestic x
merge 1:1 x using a1
drop _m
mvencode freq_domestic freq_foreign, mv(. = 0) // 把缺失值换成0
egen freq = rowtotal (freq_domestic freq_foreign) //生成行和
erase a1.dta
tw ///
bar freq_domestic x if freq < ., barw(2) ///
bcolor(blue*0.4) xsc(r(10 .)) || ///
rbar freq_domestic freq x if freq < ., barw(2) bcolor(red*0.4) ///
leg(order(1 "国产车" 2 "进口车") pos(11) col(1) ring(0)) ///
yti(频数) ///
xla(10(5)45, alt) yla(, ang(0) nogrid) ///
xsc(r(10, .)) scheme(s2mono)

等深线/等高线绘制

Stata
1
2
3
clear all
sysuse sandstone, clear
tw contour depth northing easting, scheme(s1color) levels(10) zla(#7, format(%9.1f)) yla(#7, ang(0)) crule(linear) scolor(green) ecolor(blue)

  • scolor:设定起始颜色;
  • ecolor:设定终止颜色;
  • crule:设定颜色变化的规则:hue, chue, intensity, and linear. The default is crule(hue).
  • levels:设定颜色的等级数量;
Stata
1
2
sysuse auto, clear
tw contour price weight mpg, scheme(s1color) scolor(green) ecolor(blue) levels(10) xla(11.5(6)41.5) yla(1760(616)4840, ang(0)) xti(里程数) yti(重量) zti(价格) zla(, format(%6.1f))

叠加的直方图

Stata
1
2
3
clear all
sysuse auto, clear
tw hist mpg if !foreign, start(10) width(2) freq bfcolor(none) blcolor(pink) || hist mpg if foreign, start(10) width(2) barw(1.8) freq bfcolor(none) blcolor(blue) ||, leg(order(1 "国产车" 2 "外国车") pos(2) ring(0) col(1)) yla(, ang(0) nogrid) scheme(s1mono) yti(频数) xti(里程数)

堆叠棒状图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* 产生一些数据
clear
set obs 1000
egen id = seq(), block(10)
egen time = seq(), to(10)
format time %tq
gen time2=string(time, "%tq")
gen sector = ceil(3 * runiform())
bysort id: replace sector=sector[1]
label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
label values sector sector
gen sales=exp(rnormal())*1000
gen sales2=2*sales

// reshaping the data
gen obs = _n
rename sales sales1
reshape long sales, i(obs) j(which)

gr bar (sum) sales, over(sector) over(which) ///
over(time2, label(labsize(vsmall))) ///
asyvars stack leg(size(vsmall))

堆叠条形图

Stata
1
2
3
4
webuse union, clear
keep idcode year age
recode year (71 77 = 1) (88 = 2) (* = .), gen(which)
gr bar age, over(year) over(which) asyvars stack
  • stack 的作用就是让柱体堆积起来。

分组均值离差柱状图

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
36
37
sysuse auto, clear
gr drop _all
bysort for rep78: egen mpg1 = mean(mpg)
bysort rep78: egen mpg2 = mean(mpg)

gen mpg3 = mpg1 - mpg2

tw ///
bar mpg3 rep78 if for == 0 & rep78 == 1, fc(green) ///
lc(green) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 0 & rep78 == 2, fc(blue) ///
lc(blue) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 0 & rep78 == 3, fc(red) ///
lc(red) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 0 & rep78 == 4, fc(pink) ///
lc(pink) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 0 & rep78 == 5, fc(cyan) ///
lc(cyan) barw(0.5) horiz ysc(noline) ||, ///
xti(MPG) xline(0) ///
ti("Foreign made cars") leg(off) name(k1, replace) nodraw

tw ///
bar mpg3 rep78 if for == 1 & rep78 == 1, fc(green) ///
lc(green) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 1 & rep78 == 2, fc(blue) ///
lc(blue) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 1 & rep78 == 3, fc(red) ///
lc(red) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 1 & rep78 == 4, fc(pink) ///
lc(pink) barw(0.5) horiz ysc(noline) || ///
bar mpg3 rep78 if for == 1 & rep78 == 5, fc(cyan) ///
lc(cyan) barw(0.5) horiz ysc(noline) ||, ///
xti(MPG) xline(0) ///
ti("FDomestically made cars") leg(off) name(k2, replace) nodraw

gr combine k1 k2, r(1) imargin(10 0 0 0) ycommon ///
ti("Difference from average of mpg for" "Foreign or Domestically produced cars" " ")

分组散点图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
clear all
sysuse auto, clear
tw ///
sc weight mpg if rep78 == 1 || ///
sc weight mpg if rep78 == 2 || ///
sc weight mpg if rep78 == 3 || ///
sc weight mpg if rep78 == 4 || ///
sc weight mpg if rep78 == 5, name(a1) ///
leg(row(2) label(1 "weight, rep78 == 1")) ///
leg(label(2 "weight, rep78 == 2")) ///
leg(label(3 "weight, rep78 == 3")) ///
leg(label(4 "weight, rep78 == 4")) ///
leg(label(5 "weight, rep78 == 5")) yla(, nogrid angle(0)) scheme(s1mono)

分块图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sysuse auto, clear
forval i = 10(10)40{
local m`i' = string(`i'*0.4251437, "%6.2f")
}

tw sc mpg weight, yaxis(1 2) xla(, ang(45)) ///
ysc(axis(2, alt)) ///
yla( ///
10 "`m10'" ///
20 "`m20'" ///
30 "`m30'" ///
40 "`m40'" ///
, axis(2)) ||, ///
by(foreign, title("km/L", orientation(vertical) ///
size(med) pos(3)) xrescale compact) note("") ///
yla(, ang(0) nogrid) scheme(s1mono)

改变柱条标签

Stata
1
2
3
4
sysuse auto, clear
gr bar mpg gear_ratio, showyvars ///
yvaroptions(relabel(1 "MPG" 2 "GEAR RATIO")) ///
leg(off)

Stata
1
2
3
4
5
6
sysuse auto, clear

lab def origin 0 "国产车" 1 "进口车", modify

gr hbar mpg trunk turn, over(foreign) ///
leg(r(1) span pos(6)) stack name(two, replace)

合并多幅图使用同一个图例

这里需要一个 Stata 的外部命令:

Stata
1
net install grc1leg, from("http://www.stata.com/users/vwiggins")

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
clear all

input quest str25 q a1 a2 a3 a4 a5 a6
1 "Question 1" 0 2 37 45 12 4
1 "Benchmark Q1" 2 5 25 47 17 4
2 "Question 2" 1 37 2 40 17 3
2 "Benchmark Q2" 2 5 25 47 4 17
3 "Question 3" 1 2 40 37 17 3
3 "Benchmark Q3" 2 5 25 47 17 4
4 "Question 4" 1 2 37 17 3 40
4 "Benchmark Q4" 2 5 47 25 17 4
end

gr hbar a1-a6 if quest == 1, percent over(quest, gap(1)) over(q, gap(10)) ///
stack legend(off) ysc(off) yline(20 40 60 80, lw(0.25) lc(black)) ///
saving(a1, replace) plotr(m(b+3 t+3)) outergap(30) nodraw

gr hbar a1-a6 if quest == 2, percent over(quest, gap(1)) over(q, gap(10)) ///
stack legend(off) ysc(off) yline(20 40 60 80, lw(0.25) lc(black)) ///
saving(a1a, replace) plotr(m(b+3 t+3)) outergap(30) nodraw

gr hbar a1-a6 if quest == 3, percent over(quest, gap(1)) over(q, gap(10)) ///
stack legend(off) ysc(off) yline(20 40 60 80, lw(0.25) lc(black)) ///
saving(a1b, replace) plotr(m(b+3 t+3)) outergap(30) nodraw

gr hbar a1-a6 if quest == 4, percent over(quest, gap(1)) over(q, gap(20)) ///
stack legend(span rows(1) label(1 "Missing") label(2 "Never") label(3 "Rarely") label(4 "Occassionly ") label(5 "Mostly") label(6 "Always ") size(small) pos(6)) ///
yline(20 40 60 80, lw(0.25) lc(black)) saving(a2, replace) nodraw

grc1leg a1.gph a1a.gph a1b.gph a2.gph, cols(1) ///
imargin(0 0 0 0) ycommon xcommon legendfrom(a2.gph)

绘制 45 度线

Stata
1
2
3
4
5
6
7
clear all
sysuse auto, clear
qui sum weight, mean
local max = r(max)
qui sum price, mean
if r(max) > `max' local max = r(max)
tw scatter price weight || scatteri 0 0 `max' `max', recast(line) ylabel(, nogrid ang(0)) scheme(s1mono) xti(重量) yti(价格) leg(off)

绘制一个变量相对于另外一个变量的均值走势图

Stata
1
2
3
4
5
6
7
sysuse auto, clear
collapse (mean) mpg , by(for price)

tw ///
line mpg price if for==0 || ///
line mpg price if for==1 || ///
,legend(label(2 "foreign") label(1 "domestic"))

绘制中国地图

Stata
1
2
3
4
5
6
7
8
// ssc install spmap
// github install czxa/cuse, replace
cuse china_map, clear web
save china_map, replace web
cuse china_label, clear
gen x = runiform(50, 100)
format x %9.2g
spmap x using "china_map.dta", id(id) label(label(name) xcoord(x_coord) ycoord(y_coord) size(*.66) le(30))

箭头图

Stata
1
2
3
4
5
6
7
8
9
sysuse sp500, clear
gen year = year(date)
gen month = month(date)
collapse (mean) close, by(month year)
sort year month
gen date = _n
tsset date
tw pcarrow close L.close F.close close, ///
mlabel(date) yti(当月值) xti(上月值)

控制日期标签

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
clear
input str40 time x
"5jan2011 15:00:30" 1
"1feb2011 17:30:32" 2
"1march2011 18:10:30" 6
"1april2011 05:05:01" 2
"1march2011 15:10:30" 12
end
list
gen double time1 = clock(time, "DMY hms")

local t1 = tc(1jan2011 12:00:00)
local t2 = 1000*60*60*24*10
local t3 = tc(1may2011 12:00:00)

sc x time1, xla(`t1'(`t2')`t3', format(%tcCCYY-NN-DD-HH:MM) ang(45) labsize(small)) xti("时间") msize(small) mcolor(black) yla(, angle(0) nogrid) scheme(s1mono)

连线图

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sysuse auto, clear
gsort -mpg
keep in 1/25
replace mpg = round(mpg)
gen rank1 = _n
egen rank2 = rank(-price)

gen byte one = 1
gen byte two = 2
gen left = 0.4

tw ///
pcspike rank1 one rank2 two, ///
xla(none) xsc(noline r(0.3 2.3)) ///
xti("") ysc(r(-1 .) reverse off) || ///
sc rank1 one, mlabel(mpg) mlabsize(small) mlabpos(9) ms(none) || ///
sc rank2 two, mlabel(price) mlabsize(small) mlabpos(3) ms(none) || ///
sc rank1 left, mla(make) mlabsize(small) mlabpos(3) ms(none) ///
text(-0.5 1 "mpg, US gallons") ///
text(-0.5 2 "price, {c S|}US in 1978") ///
leg(off)

两行坐标轴标签

Stata
1
2
3
4
5
6
7
webuse turksales, clear
line sales t, ///
xtick(119.5(4)159.5, tlength(*1.5)) ///
xmlabel(120/159, noticks format(%tqq)) ///
xla(121.5(4)157.5, noticks format(%tqCY) ///
labgap(2.5)) xti("") yla(, nogrid ang(0)) ///
scheme(s1mono)

平行坐标轴图

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
* `=char(34)': 34是双引号的ASCII码
sysuse auto, clear
drop make
local a
local z = 1
foreach i of varlist _all{
sum `i', d
local a `"`a' text(1.2 `z' `=char(34)'`i'`=char(34)'`=char(34)'`=string(`r(max)', "%8.2f")'`=char(34)', orientation(vertical) placement(n) size(3) justification(left)) text(-0.1 `z' `=char(34)'`i'`=char(34)'`=char(34)'`=string(`r(min)', "%8.2f")'`=char(34)', orientation(vertical) placement(south) size(3) justification(right))"'
gen x`z' = (`i' - r(min))/(r(max) - r(min))
local ++z
}
drop if rep78 == .
gen id = _n

reshape long x, i(id) j(type)
drop if x > 100

tw ///
line x type if for == 1, lcolor(red*0.8) c(L) || ///
line x type if for == 0, lcolor(blue*0.8) c(L) ///
xline(1/12) ///
ysc(off r(0 1.1)) xsc(off) ///
text(0 0 "MIN") text(1 0 "MAX") ///
graphr(m(t = 20 b = 20)) leg(off) `a'

# Stata

评论

程振兴

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

Your browser is out-of-date!

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

×