superscatter:散点图+分布图

superscatter:散点图+分布图

这个是我觉得Stata里面最漂亮且强大的绘图命令了!可以非常充分地展示两个变量之间的关系。

安装

Stata
1
net install superscatter, from(http://digital.cgdev.org/doc/stata/MO/Misc)

基础用法

Stata
1
2
3
4
5
6
7
sysuse lifeexp, clear
qui gen loggnp = log10(gnppc)
label var loggnp "Log base 10 of GNP per capita"
describe
summarize, detail
* 绘图:在侧边添加直方图
superscatter lexp loggnp, name(_example1, replace)

改变直方图的填充色和纵轴使用百分比

Stata
1
superscatter lexp loggnp, percent color(dkorange) m(s) msize(small) xtitle("GNP per capita (log)") name(_example2, replace)

把直方图换成核密度分布图并添加中位线和散点计数

Stata
1
superscatter lexp loggnp, kdensity medians tabulate(count) detail name(_example3, replace)

添加拟合线和置信区间

Stata
1
2
3
4
* 查看返回值
return list
* 添加拟合线和置信区间.
superscatter lexp loggnp, means fittype(lfitci) fitoptions(lwidth(vthick)) legend(ring(0) cols(1) pos(5)) ytitle(Life Expectancy) name(_example4, replace)

添加置信椭圆

Stata
1
superscatter lexp loggnp, ellipse quartiles ytitle(Life Expectancy) name(_example5, replace)

添加置信椭圆和拟合线

Stata
1
2
3
* 查看返回值
return list
superscatter lexp loggnp, means fittype(lfit) fitoptions(lwidth(vthick)) ellipse ytitle(Life Expectancy) name(_example6, replace)

添加45度线比较预测值和真实值

Stata
1
2
3
4
5
6
reg lexp loggnp
predict lexp_hat, xb
lab var lexp_hat "Fitted value of life expectancy"
local textadds `"text(60 75 "Observed less" "than predicted") text(78 68 "Observed greater" "than predicted") "'

superscatter lexp_hat lexp, line45 legend(off) mlabel(country) ytitle(Observed Life Expectancy) xtitle(Predicted Life Expectancy) `textadds' name(_example7, replace)

使用平方根轴

Stata
1
superscatter lexp gnp, sqrt ytitle(Life Expectancy on square-root scale) xtitle(GNP per capita onsquare-root scale) name(_example8, replace)

使用对数轴

这里是使用以10为底的对数再加1(确保结果为正)的轴:

Stata
1
superscatter popgrowth gnp, log10plusone ytitle(Population growth rates > -1 on log scale) xtitle(GNP per capita on log scale) name(_example9, replace)

强调和隐藏某些散点

Stata
1
superscatter lexp gnp, hilite(region==2) hide(region==2) sqrt ylabel(7.0 "49" 8 "64" 9 "81") legend(order(1 "North America" 2 "Other regions") ring(0) pos(4) col(1))

在对话框中操作

Stata
1
db superscatter

# Stata

评论

程振兴

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

Your browser is out-of-date!

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

×