Stata 绘图技巧总结(四)

Stata 绘图技巧总结(四)

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

覆盖图

Stata
1
2
vguse allstates, clear
tw sc propval100 rent700 urban, ms(Oh t)

Stata
1
tw sc propval100 rent700 urban, mstyle(p2 p8)

Stata
1
2
3
tw sc propval100 rent700 urban || ///
lfit propval100 urban || ///
lfit rent700 urban

pcycle()

Stata
1
2
3
tw sc propval100 rent700 urban || ///
lfit propval100 urban || ///
lfit rent700 urban ||, pcycle(2)

Stata
1
2
sysuse sp500, clear
tw line high low close date, sort lw(thick thick .)

Stata
1
tw line high low close date, sort lstyle(p1 p1 p2)

leg(span)

允许图例宽于绘图区域:

Stata
1
2
3
4
5
6
7
8
sysuse sp500, clear
volume = volume/1000
tw rbar open close date || ///
rspike high low date || ///
spike volume date, yaxis(2) ||, ///
leg(span) ysc(range(500 1400) axis(1)) ///
ysc(range(0 50) axis(2)) sch(vg_brite) ///
yla(900(250)1400, axis(1))

改变点的外观

Stata
1
2
vguse allstates, clear
tw sc ownhome borninstate, ms(S)

Stata
1
tw sc ownhome borninstate, ms(s)

Stata
1
tw sc ownhome borninstate, ms(X)

Stata
1
tw sc ownhome borninstate, ms(sh)

Stata
1
tw sc ownhome borninstate, ms(Sh)

Stata
1
tw sc ownhome borninstate, ms(+)

Stata
1
tw sc ownhome borninstate, ms(p)

Stata
1
tw sc ownhome propval100 borninstate, ms(t Oh)

Stata
1
tw sc ownhome propval100 borninstate, ms(. Oh)

Stata
1
tw sc ownhome propval100 borninstate, mlabel(stateab) mlabpos(center) ms(i)

Stata
1
tw sc ownhome borninstate [aw = propval100], ms(oh)

Stata
1
tw sc ownhome borninstate [aw = propval100], ms(oh) msize(large) mlabel(stateab)

Stata
1
tw sc ownhome borninstate [aw = propval100], ms(oh) msize(large) || sc ownhome borninstate, mlabel(stateab) ms(i) mlabpos(center)

Stata
1
tw sc ownhome borninstate, mfc(ltblue) mlc(navy)

Stata
1
tw sc ownhome borninstate, mstyle(p1)

Stata
1
tw sc ownhome borninstate, mstyle(p2)

Stata
1
tw sc ownhome propval100 borninstate, mstyle(p1 p10) msize(. medium)

改变图形的外观

Stata
1
vguse allstates, clear

angle

Stata
1
sc workers2 faminc, mlabel(stateab) mlabangle(45)

color

Stata
1
sc workers2 faminc, mc(gs8)

Stata
1
sc workers2 faminc, mc(lavender)

Stata
1
vgcolormap

Stata
1
sc workers2 faminc, mc("255 255 0")

Stata
1
sc workers2 faminc, mc("255 150 100")

placement: 罗盘方向 n/s/e/w/nw/ne/sw/se

Stata
1
sc workers2 faminc, ti("Work Status and Income", ring(0) placement(se))

lp:线形

Stata
1
2
3
4
vguse spjanfeb2001, clear
tw line close tradeday, lp(solid) sort || ///
lfit close tradeday, lp(dash) sort || ///
lowess close tradeday, lp(shortdash_dot)

Stata
1
2
3
tw line close tradeday, lp("l") sort || ///
lfit close tradeday, lp("._") || ///
lowess close tradeday, lp("-###")

Stata
1
2
3
tw line close tradeday, lp("l") sort || ///
lfit close tradeday, lp(" ##") || ///
lowess close tradeday, lp("-.#")

查看所有的线型

Stata
1
palette linepalette

查看所有的点型

Stata
1
palette symbolpalette

查看所有的特殊文本

Stata
1
palette smclsymbolpalette

查看某个颜色的样式

Stata
1
palette color dkorange

margin:边距

Stata
1
2
vguse allstates, clear
sc workers2 faminc, ti("Overall title", margin(large) box)

  • margin(top_bottom):medium on the top and the bottom but zero on the left and right.
Stata
1
sc workers2 faminc, ti("Overall title", margin(top_bottom) box)

  • margin(sides): medium on the left and right but zero on the top and bottom.
Stata
1
sc workers2 faminc, ti("Overall title", margin(sides) box)

margin(l r b t)

Stata
1
sc workers2 faminc, ti("Overall title", margin(9 6 3 0) box)

标签大小

Stata
1
sc propval100 rent700 ownhome urban, msize(vsmall medium large)

Stata
1
sc propval100 rent700 ownhome urban, msize(*0.5 *1 *1.5)

orientation

Stata
1
sc workers2 faminc, yti("Family" "Worker" "Status", orientation(horiz))

Stata
1
sc workers2 faminc, yti("Family" "Worker" "Status", orientation(verti))

点型

Stata
1
tw sc propval100 rent700 ownhome urban, ms(S T O)

Stata
1
tw sc propval100 rent700 ownhome urban, ms(s t o)

Stata
1
tw sc propval100 rent700 ownhome urban, ms(Sh Th Oh)

分组绘图

holes

Stata
1
2
vguse allstates, clear
tw sc ownhome borninstate, by(north, total holes(2))

iscale

Stata
1
tw sc ownhome borninstate, by(north, total iscale(*1.5))

compact

Stata
1
tw sc ownhome borninstate, by(north, total compact)

noedgelabel: 只在最后一行显示 x 轴

Stata
1
tw sc ownhome borninstate, by(north, total noedgelabel)

yrescale: 为每组使用单独的 y 轴

Stata
1
tw sc ownhome borninstate, by(north, yrescale)

xrescale: 为每组使用单独的 x 轴

Stata
1
tw sc ownhome borninstate, by(north, xrescale)

rescale: 为每组使用单独的坐标系

Stata
1
tw sc ownhome borninstate, by(north, rescale)

iyaxes: 为每个组显示 y 轴

Stata
1
tw sc ownhome borninstate, by(north, iyaxes)

ixaxes: 为每个组显示 x 轴

Stata
1
tw sc ownhome borninstate, by(north, ixaxes cols(1))

iytitle/ixtitle

Stata
1
tw sc ownhome borninstate, by(north, total iytitle iyaxes)

title

Stata
1
tw sc ownhome borninstate, by(north) ti("My title")

Stata
1
tw sc ownhome borninstate, by(north, ti("My title"))

subtitle

Stata
1
tw sc ownhome borninstate, by(north) subtitle("Region of state", prefix)

note

Stata
1
tw sc ownhome borninstate, by(north, note(""))

subtitle-pos

Stata
1
tw sc ownhome borninstate, by(north, total) subtitle(, pos(5) ring(0) nobexpand)

使用图例

Stata
1
tw sc borninstate propval100 ownhome, by(nsw) leg(label(1 "Born in state") label(2 "% > 100K"))

Stata
1
tw sc borninstate propval100 ownhome, by(nsw, leg(pos(6))) leg(label(1 "Born in state") label(2 "% > 100K")  rows(1))

Stata
1
tw sc ownhome borninstate, by(north, total rescale ixtitle iytitle b1title("") l1title(""))

分块图

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)

范围图

rconnected

Stata
1
2
3
4
5
6
7
8
9
sysuse sp500, clear
tw rconnected high low date in 1/40, sort ///
msize(vsmall) ///
horiz ///
yla(#4) ///
ms(Oh) ///
mcolor(lavender) ///
lp(dash) ///
lc(dkgreen)

rline

Stata
1
2
3
4
tw rline high low date, sort ///
lc(orange) ///
lw(thick) ///
lp(dash)

rarea

Stata
1
2
tw rarea high low date, sort ///
bc(pink) lc(gold) lw(thick) fc(teel)

rcap

Stata
1
2
3
4
tw rcap high low date in 1/20, sort ///
msize(small) ///
lc(cranberry) ///
lw(thick)

rspike

Stata
1
tw rspike high low date in 1/20, sort

rcapsym

Stata
1
2
3
4
tw rcapsym high low date in 1/20, ///
msize(small) ///
ms(Oh) ///
lw(thick)

rbar

Stata
1
tw rbar high low date in 1/40, barw(0.4) bc(sienna) fc(sienna) lc(cranberry) lw(thick)

rscatter

Stata
1
2
3
4
tw rscatter high low date in 1/50, ///
msize(small) ///
mcolor(red) ///
ms(Sh)

吊灯图

  • 非常适合处理效果的可视化
  • 首先制作一些数据
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 76
gen treatment=mod(_n,3)+1

set seed 12345
gen w_before=rnormal(82, 4.8)
replace w_before=rnormal(81, 5.7) if treatment==2
replace w_before=rnormal(83, 5) if treatment==3

gen w_after=rnormal(85.6, 8.3)
replace w_after=rnormal(81.1, 4.7) if treatment==2
replace w_after=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
Stata
1
vplplot w_before w_after

Stata
1
2
vplplot w_before w_after, sort(w_before)
`

  • diff specifies that the y axis should show the difference between yvar1 and yvar2, i.e. yvar1 - yvar2.
  • mean specifies that the x axis should show the mean of yvar1 and yvar2.
Stata
1
vplplot w_before w_after, diff mean

  • ratio specifies that the y axis should show the ratio of yvar1 and yvar2, i.e. yvar1 / yvar2.
  • base(#) specifies a baseline for comparison if either diff or ratio is specified. With diff, base has default 0. With ratio, base has default 1.
Stata
1
vplplot w_before w_after, ratio base(2)

点图

指定变量和分组

Stata
1
2
vguse nlsw, clear
gr dot tenure, over(occ7)

Stata
1
gr dot tenure, over(occ7) over(collgrad)

Stata
1
gr dot tenure, over(occ7) over(collgrad) over(married)

Stata
1
gr dot tenure, over(married) over(occ7) over(collgrad)

Stata
1
gr dot prev_exp tenure, over(occ7)

Stata
1
gr dot (p10) wage (p25) wage (p50) wage (p75) wage (p90) wage, over(occ7)

控制点距

Stata
1
gr dot prev_exp tenure, over(occ7) linegap(30)

Stata
1
gr dot tenure, over(occ5) over(collgrad) over(married) linegap(70) leg(r(1) span pos(6))

控制 y 轴

Stata
1
gr dot hours, over(occ7) yla(30(5)45) exclude0

控制点的外观

Stata
1
gr dot prev_exp tenure, over(occ7) dots(msymbol(Oh) msize(medium) mcolor(dkgreen))

Stata
1
gr dot prev_exp tenure, over(occ7) linetype(line) lines(lw(thick) lc(erose))

Stata
1
gr dot prev_exp tenure, over(occ7) linetype(rectangle) rwidth(3) rectangles(fc(erose) lc(maroon))

Stata
1
gr dot prev_exp tenure, over(occ7) marker(1, msymbol(D) mc(teal) msize(large))

Stata
1
gr dot prev_exp tenure, over(occ7) marker(1, msymbol(d) mfc(teal) mlc(dkgreen) mlw(thick)) marker(2, msymbol(S) mfc(ltblue) mlcolor(blue) mlw(thick))

等高等深图

Stata
1
2
sysuse sandstone, clear
tw contour depth northing easting

levels()

Stata
1
tw contour depth northing easting, levels(15)

ccuts()

Stata
1
tw contour depth northing easting, ccuts(7500(100)8100)

ccolors()

Stata
1
2
3
tw contour depth northing easting, ///
ccuts(7500(100)8100) ///
ccolors(red green blue pink orange purple yellow) sch(vg_s2c)

Stata
1
2
3
tw contour depth northing easting, ///
ccuts(7500(100)8100) ///
ccolors(red green blue pink) sch(vg_s2c)

crule() scolor() ecolor()

Stata
1
tw contour depth northing easting, crule(linear) sc(red) ec(yellow) levels(20)

Stata
1
tw contour depth northing easting, crule(intensity) levels(10)

Stata
1
tw contour depth northing easting, crule(intensity) ecolor(dkorange) levels(10)

Stata
1
tw contour depth northing easting, crule(hue) scolor(purple) ecolor(dkorange) levels(10)

Stata
1
tw contour depth northing easting, crule(chue) scolor(purple) ecolor(dkorange) levels(10)

contourline

Stata
1
tw contourline depth northing easting

Stata
1
tw contourline depth northing easting, colorlines

构建图像

  • 显示一幅已经关闭的图
    Stata
    1
    gr di
Stata
1
2
3
vguse allstates, clear
set scheme vg_past
gr bar propval100, over(nsw) over(division)

Stata
1
2
gr bar propval100, over(nsw) over(division) ///
nofill

  • nofill: show only the combinations of nsw and division.
Stata
1
2
3
gr bar propval100, over(nsw) over(division) ///
nofill ///
asyvars

  • asyvars: specifies that the first over() group be treated as yvars.
Stata
1
2
3
4
5
6
7
8
9
gr bar propval100, over(nsw) over(division, ///
label(ang(45)))///
nofill ///
asyvars ///
yti("% homes over $100K") ///
yla(0(10)80, ang(0)) ///
b1ti(Region) ///
leg(r(1) pos(1) ring(0)) ///
blabel(bar, format(%4.2f) pos(outside))
  • ring(0): 控制图例位于绘图区域内
  • blabel(bar, format(%4.2f) pos(outside)): 在柱条上方添加数字标签

# Stata

评论

程振兴

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

Your browser is out-of-date!

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

×