Stata绘图中的阴影的使用

Stata绘图中的阴影的使用

本文是旧笔记的汇总,本文汇总了Stata绘图中的阴影的使用。

使用函数绘制阴影区间和栅栏线

Stata
1
2
3
4
5
6
7
8
9
10
11
clear all
sysuse auto, clear
gsort mpg
gen x = _n
gen max = ceil(_N/10)*10
sum mpg, d
tw ///
area max x if inrange(x, `=r(p1)', `=r(p25)'), lcolor(bg) fcolor(green) fintensity(inten20) || ///
area max x if inrange(x, `=r(p75)', `=r(p99)'), lcolor(bg) fcolor(green) fintensity(inten20) || ///
line x mpg, legend(off) plotr(margin(zero)) xtitle("车辆使用某定量燃料可行驶的英里数") xla(10 `=r(p1)' `=r(p25)' `=r(p75)' `=r(p99)') ysc(range(0 `=_N')) yla(, angle(0) nogrid) scheme(s2mono)
graph export 带大幅阴影的图.png, replace

先绘制核密度曲线,然后再用area绘制阴影部分

Stata
1
2
3
4
5
6
sysuse auto, clear
sum price, mean
local mean = r(mean)
kdensity price, gen(x h)
format h %6.4f
tw line h x || area h x if x < `mean', color(ltkhaki) yla(, angle(0) nogrid) ytitle(价格密度) xtitle(价格) legend(label(1 "价格的密度曲线") label(2 "小于均值的价格")) scheme(s1mono) xlabel(0(2000)20000)

twoway area命令

水平

Stata
1
2
sysuse sp500, clear
tw area close date, sort

竖直

Stata
1
tw area close date, horiz yla(#3)

base()

Stata
1
tw area close date, base(1150)

bcolor()

Stata
1
tw area close date, bc(khaki)

area/rarea命令

  • area所绘图形是曲线和平行于x轴的直线
  • rarea所绘图形是曲线和曲线、曲线和任意直线所围成的
Stata
1
2
3
4
5
6
7
clear all
set obs 5000
gen x = uniform()*8
replace x = . if x < 1
gen y = x^2
tw area y x, sort base(10) cmissing(y|n) fcolor(green*0.4) lcolor(red*0.8) title("y = x{superscript:2}的函数图像") ytitle("y", angle(0)) xtitle() xlabel(0(2)8 5) ylabel(0(20)60 10 27, ang(0)) legend(off) text(27 5 "y = x{superscript:2}") scheme(s1color)
graph export 函数图像.png, replace
  • cmissing(y|n)是指若变量中有缺失值则忽略使变量连续
  • base(#)一般默认命令是:area所绘图形是与x轴即y=0所围成的部分为阴影面积,此处base(0)就相当于设置y=0.
  • sort对变量x进行排序

Stata
1
2
tw area y x, horizontal sort nodropbase cmissing(y|n) fcolor(green*0.8) lcolor(yellow*0.8) ylabel(, angle(0)) text(5.5 27 "y = x{superscript:2}")
graph export 函数图像1.png, replace
  • nodropbase选项的含义是将y的第一个值和最后一个值连线围成的部分为阴影面积

Stata
1
2
3
4
5
6
7
8
9
10
clear all
set obs 5000
gen x1 = uniform()
gen x2 = uniform()*4 + 1
gen y1 = x1^2
gen y2 = 2-x1
gen y3 = sin(x2)
gen y4 = x2^2
tw rarea y1 y2 x1, sort fcolor(green*0.8) lcolor(black*0.8) || rarea y3 y4 x2, sort fcolor(blue*0.8) lcolor(black*0.8) legend(off) scheme(s1mono) yla(, angle(0) nogrid)
graph export 函数图像2.png, replace

Stata
1
2
3
4
5
sysuse auto, clear
reg price mpg
predictnl pprice = predict(xb), ci(max95 min95) l(95)
tw rarea max95 min95 mpg, sort color(gs12) || line pprice mpg, legend(off) ||, yla(, ang(0)) xti(里程数) xla(10(5)40, alt)
graph export 函数图像3.png, replace

  • predictnl: 在估计后获取非线性预测、标准误等。
# Stata

评论

Your browser is out-of-date!

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

×