不惧困难 发表于 2019-6-12 16:07:01

Stata绘图:简单好用的37条外部命令


aaplot
用于散点的线性或二次拟合,显示拟合方程及R方

示例:
*安装
ssc install aaplot
*导入1978年汽车交易的数据
sysuse auto, clear
gen gpm = 1000 / mpg
label var gpm "Gallons per thousand miles"
*拟合
aaplot gpm weight, name(plot)

beamplot
用于以均值为支点的“跷跷板”图示

示例:
*安装
ssc install beamplot
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制以均值为支点的“跷跷板”
beamplot mpg, by(foreign) over(rep78)

bihist
绘制双变量双向直方图

示例:
*安装
ssc install bihist
*导入1978年汽车交易的数据
sysuse auto, clear
*以汽车里程数分组,绘制车型的双向直方图
bihist mpg, by(foreign)

binscatter
解决大样本情况下,散点图过于拥挤无法直观解释的问题

示例:
*安装
ssc install binscatter
*导入1988年女性调查的数据
sysuse nlsw88, clear
keep if inrange(age,35,44) & inrange(race,1,2)
*工作年限和工资之间的关系
scatter wage tenure,title("Graph produced by Scatter") name(plot1)
binscatter wage tenure
*二次拟合
binscatter wage tenure, line(qfit) xscale(range(0,25)) xlabel(0(5)25) ylabel(0(10)40) yscale(range(0,40)) title("Graph produced by Binscatter") name(plot2)
graph combine plot1 plot2

byhist
绘制双变量单向直方图

示例:
*安装
ssc install byhist
*导入1978年汽车交易的数据
sysuse auto, clear
*以汽车里程数分组,绘制车型的单向直方图
byhist mpg, by(foreign)

catplot
显示变量的频率或百分比


示例:
*安装
ssc install catplot
*导入1978年汽车交易的数据
sysuse auto, clear
*指定分类
gen himpg = mpg > 25
label def himpg 1 "mpg > 25" 0 "mpg <= 25"
label val himpg himpg
*绘制直方图
catplot himpg rep78 foreign
*绘制直方图
catplotrep78, over(for) stack asyvarsperc(for) blabel(bar, position(center) format(%3.1f)) legend(off)

cbarplot
显示频率的中心条状图

示例:
*安装
ssc install cbarplot
*输入数据
clear
input levels freqcores freqblanks freqtools
25 21 32 70
24 36 52 115
23 126 650 549
22 159 2342 1633
21 75 487 511
20 176 1090 912
19 132 713 578
18 46 374 266
17 550 6182 1541
16 76 846 349
15 17 182 51
14 4 51 14
13 29 228 130
12 135 2227 729
end
reshape long freq, i(levels) j(kind) string
*以百分比形式显示频率
cbarplot levels kind , percent(levels)

cdfplot
绘制样本累积分布函数

示例:
*安装
ssc install cdfplot
*导入1978年汽车交易的数据
sysuse auto, clear
*以车长分组,为车型的值指定单独的样本累积分布函数
cdfplot length , by(foreign) norm saving(mygraph,replace)

ciplot
显示均值和置信区间

示例:
*安装
ssc install ciplot
*导入美国城市的气温数据
sysuse citytemp, clear
*绘制置信区间
ciplot heatdd cooldd, by(division) xla(, ang(45))
cmogram

以某变量为条件,显示另一变量的均值、中位数、频数、比例

示例:
*安装
ssc install cmogram
*导入1978年汽车交易的数据
sysuse auto, clear
*以车重为条件,控制汽车价格,显示汽车里程数的均值
cmogram mpg weight, histopts(bin(5)) lfit cutpoint(3250) lineat(3000 3250 3500) controls(price)


cpyxplot
绘制指定变量之间的交叉散点图

示例:
*安装
ssc install cpyxplot
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制车型与维修记录、与里程数、与车圈、与车长的散点图
cpyxplot foreign \rep78 mpg turn length

cvxhull
绘制凸包图

示例:
*安装
ssc install cvxhull
*导入1978年汽车交易的数据
sysuse auto, clear
gen foreign1=foreign
label define for 1 For 0 Dom
label valuesforeign1 for
*在散点图中显示汽车里程数的范围及每一个标有组值的点,每组观测值将计算出最多2层hull并共享一个组值
cvxhullmpg weight, group(foreign) hulls(2) scat(mlab(foreign1) mlabpos(c) msym(i) ysc(r(0,60)))

cycleplot
把时间序列数据中每个周期的值垂直地绘制出来

示例:
*安装
ssc install cycleplot
*导入美国GNP数据
use "D:\us_gnp.dta", clear
*按季度分解
cycleplot gnp96 quarter year, length(4)

devnplot
显示数据的平均值偏离

示例:
*安装
ssc install devnplot
*导入1978年汽车交易的数据
sysuse auto, clear
*依维修记录分组标记汽车里程数的数值及其均值
devnplot mpg rep78

drarea
用不同颜色解决区域重叠覆盖的问题

示例:
*安装
ssc install drarea
*导入标普500指数数据
sysuse sp500, clear
generate high2 = high+15*uniform()
generate low2 = low+15*uniform()
*分颜色绘图
drarea high low high2 low2 date in 1/20

eclplot
标记置信区间

示例:
*安装
ssc install eclplot
ssc install parmest
ssc install sencode
*导入1978年汽车交易的数据
sysuse auto, clear
*创建一个输出数据集
parmby "xi:regress mpg i.foreign i.rep78", label norestore
*创建一个带有值标签的数值变量
sencode parm,gene(parmid)
*使用参数标签来标记垂直置信区间
eclplot estimate min95 max95 parmid

ellip
绘制置信椭圆

示例:
*安装
ssc install ellip
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制置信椭圆
ellip mpg weight, by(foreign, total legend(off)) total tlabel(Total as a by-group) plot(scatter mpg weight)

grcomb
组合相同类型的多个图

示例:
*安装
ssc install grcomb
*导入1978年汽车交易的数据
sysuse auto, clear
*同时显示汽车价格、里程数、维修记录及发动机排量的箱线图
grcomb graph box price mpg rep78 displacement, v(1)

hangroot
检查数据分布

示例:
*安装
ssc install hangroot
*导入1988年女性调查的数据
sysuse nlsw88, clear
*绘制收入的分布曲线
hangroot wage,bar

hmap
绘制热图

示例:
*安装
ssc install hmap
*输入数据
clear all
set obs 64
generate n=int(uniform()*10)
generate x=1+int((_n-1)/8)
generate y=1+mod((_n-1),8)
label define xlab 1 "one" 2 "two" 7 "seven" 8 "eight"
label define ylab 3 "three" 4 "four" 5 "five" 6 "six"
label value x xlab
label value y ylab
table y x
*绘制热图
hmap x y n

linkplot
显示成对数据的结构

示例:
*安装
ssc install linkplot
*输入10个男孩的A和B材料鞋子的数据(Box,Hunter and Hunter,1978)
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
rename A wearA
rename B wearB
reshape long wear, string i(id) j(j)
encode j, gen(material)
*链接成对数据
linkplot material wear, link(id) yla(1 2, valuelabel) ysc(r(0.5 2.5)) yla(, ang(h))

netplot
生成社会网络分析的网络图

示例:
*安装
ssc install netplot
*导入社会网络数据
sysuse network1a,clear
*生成网络图
netplot x_c y_c, label

pdplot
绘制帕累托图

示例:
*安装
ssc install pdplot
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制汽车里程数的帕累托图
pdplot mpg

plotmatrix
绘制矩阵图

示例:
*安装
ssc install plotmatrix
*导入1978年汽车交易的数据
sysuse auto, clear
*线性回归
reg price mpg trunk weight length turn, nocons
mat regmat = e(V)
*绘制矩阵
plotmatrix, m(regmat) c(red) ylabel(,angle(0))

sixplot
显示单变量的六种数据诊断及描述性统计图

示例:
*安装
ssc install sixplot
*导入预期寿命数据
sysuse uslifeexp,clear
*生成男性预期寿命的统计图
sixplot le_male

spineplot
使图的宽度与频率呈比例

示例:
*安装
ssc install spineplot
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制直方图
spineplot foreign rep78

spmap
绘制等值线地图、比例符号地图等地图

示例:
*安装
ssc install spmap
*导入意大利地图数据
use "D:\Italy-RegionsData.dta", clear
*绘制意大利地图
spmap relig1 using "D:\Italy-RegionsCoordinates.dta", id(id)

statplot
在不借助图例的情况下,使用坐标轴标记数据

示例:
*安装
ssc install statplot
*导入人口调查的数据
sysuse census, clear
*显示不同地区的结婚数与离婚数
statplot marriage divorce, over(region) s(sum) xpose

stripplot
绘制航线图



示例:
*安装
ssc install stripplot
*导入血压数据
sysuse bplong, clear
egen group = group(age sex), label
*绘制航线图
stripplot bp*, bar over(when) by(group, compact col(1) note("")) ysc(reverse) subtitle(, pos(9) ring(1) nobexpand bcolor(none) placement(e)) ytitle("") xtitle(Blood pressure (mm Hg))
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制航线图
stripplot mpg, over(rep78) stack h(0.5) bar(lcolor(red))
*绘制航线图
gen pipe = "|"
stripplot price, over(rep78) box(barw(0.3)) ms(none) mla(pipe) boffset(0.3)

tabplot
直方图的列联表示

示例:
*安装
ssc install tabplot
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制车型与维修记录的列联表
tabplot foreign rep78, percent(foreign)

tddens
绘制网格上的双变量核密度图

示例:
*安装
ssc install tddens
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制汽车价格和里程数的双变量核密度图
tddens price mpg, s b

triplot
绘制三个变量的百分比

示例:
*安装
ssc install triplot
*输入数据
clear
input a1 a2 a3 str10 name
0.10 0.10 0.80 John
0.80 0.10 0.10 Fred
0.25 0.25 0.50 Jane
0.90 0.5 0.5 Helen
0.10 0.20 0.70 Ed
0.50 0.25 0.25 Kate
0.20 0.60 0.20 Michael
0.25 0.25 0.50 Darren
0.5 0.90 0.5 Samar
end
*绘制三角图
triplot a1a2a3, mlab(name)

vioplot
绘制小提琴图

示例:
*安装
ssc install vioplot
*导入1978年汽车交易的数据
sysuse auto, clear
*绘制小提琴图
vioplot mpg, over(rep78) horizontal name(myplot)

zmap
定义笛卡尔坐标,特别针对空间数据的处理

示例:
*安装
ssc install zmap
*导入1988年女性调查的数据
sysuse nlsw88, clear
*生成工资、当前职级与年龄的直角坐标系
zmap wage age grade, ms(S ..) ysc(on) xsc(on)
页: [1]
查看完整版本: Stata绘图:简单好用的37条外部命令