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
3
vguse allstates, clear
tw sc ownhome borninstate, by(north, total holes(2))
gre vfzht

iscale

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

compact

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

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

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

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

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

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

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

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

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

iyaxes: 为每个组显示y轴

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

ixaxes: 为每个组显示x轴

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

iytitle/ixtitle

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

title

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

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

subtitle

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

note

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

subtitle-pos

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

使用图例

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

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

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

分块图

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
10
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)
gr export fwt1.png, replace

rline

Stata
1
2
3
4
5
tw rline high low date, sort ///
lc(orange) ///
lw(thick) ///
lp(dash)
gr export fwt2.png, replace

rarea

Stata
1
2
3
tw rarea high low date, sort ///
bc(pink) lc(gold) lw(thick) fc(teel)
gr export fwt3.png, replace

rcap

Stata
1
2
3
4
5
tw rcap high low date in 1/20, sort ///
msize(small) ///
lc(cranberry) ///
lw(thick)
gr export fwt4.png, replace

rspike

Stata
1
2
tw rspike high low date in 1/20, sort
gr export fwt5.png, replace

rcapsym

Stata
1
2
3
4
5
tw rcapsym high low date in 1/20, ///
msize(small) ///
ms(Oh) ///
lw(thick)
gr export fwt6.png, replace

rbar

Stata
1
2
tw rbar high low date in 1/40, barw(0.4) bc(sienna) fc(sienna) lc(cranberry) lw(thick)
gr export fwt7.png, replace

rscatter

Stata
1
2
3
4
5
tw rscatter high low date in 1/50, ///
msize(small) ///
mcolor(red) ///
ms(Sh)
gr export fwt8.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
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
2
vplplot w_before w_after
gr export vplplot.png, replace

Stata
1
2
3
vplplot w_before w_after, sort(w_before)
gr export vplplot1.png, replace
`

  • 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
2
vplplot w_before w_after, diff mean
gr export vplplot2.png, replace

  • 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
2
vplplot w_before w_after, ratio base(2)
gr export vplplot3.png, replace

点图

指定变量和分组

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

Stata
1
2
gr dot tenure, over(occ7) over(collgrad)
gre vdot1

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

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

Stata
1
2
gr dot prev_exp tenure, over(occ7)
gre vdot4

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

控制点距

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

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

控制y轴

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

控制点的外观

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

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

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

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

Stata
1
2
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))
gre vdot13

等高等深图

Stata
1
2
3
sysuse sandstone, clear
tw contour depth northing easting
gr export dgt1.png, replace

levels()

Stata
1
2
tw contour depth northing easting, levels(15)
gr export dgt2.png, replace

ccuts()

Stata
1
2
tw contour depth northing easting, ccuts(7500(100)8100)
gr export dgt3.png, replace

ccolors()

Stata
1
2
3
4
tw contour depth northing easting, ///
ccuts(7500(100)8100) ///
ccolors(red green blue pink orange purple yellow) sch(vg_s2c)
gr export dgt4.png, replace

Stata
1
2
3
4
tw contour depth northing easting, ///
ccuts(7500(100)8100) ///
ccolors(red green blue pink) sch(vg_s2c)
gr export dgt5.png, replace

crule() scolor() ecolor()

Stata
1
2
tw contour depth northing easting, crule(linear) sc(red) ec(yellow) levels(20)
gr export dgt6.png, replace

Stata
1
2
tw contour depth northing easting, crule(intensity) levels(10)
gr export dgt7.png, replace

Stata
1
2
tw contour depth northing easting, crule(intensity) ecolor(dkorange) levels(10)
gr export dgt8.png, replace

Stata
1
2
tw contour depth northing easting, crule(hue) scolor(purple) ecolor(dkorange) levels(10)
gr export dgt9.png, replace

Stata
1
2
tw contour depth northing easting, crule(chue) scolor(purple) ecolor(dkorange) levels(10)
gr export dgt10.png, replace

contourline

Stata
1
2
tw contourline depth northing easting
gr export dgt11.png, replace

Stata
1
2
tw contourline depth northing easting, colorlines
gr export dgt12.png, replace

构建图像

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

Stata
1
2
3
gr bar propval100, over(nsw) over(division) ///
nofill
gr export picg2.png, replace

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

  • asyvars: specifies that the first over() group be treated as yvars.
Stata
1
2
3
4
5
6
7
8
9
10
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))
gr export picg4.png, replace
  • ring(0): 控制图例位于绘图区域内
  • blabel(bar, format(%4.2f) pos(outside)): 在柱条上方添加数字标签

# Stata

评论

程振兴

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

Your browser is out-of-date!

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

×