如何使用 Stata 绘制四象限坐标图?

今天有个培训班的小伙伴问了关于绘制四象限坐标图的问题,通常我们使用 Stata 绘制的散点图是类似这样的:

图一

如果我们想绘制这样的四象限坐标图该怎么做呢: 图二

上面的这个图就是 Stata 绘制的,这个图的绘制思路是在图一的基础上绘制两条相互垂直且都经过原点的坐标轴,使用 text() 添加轴标签,最后再使用 xsc(off) 和 ysc(off) 去除原有的坐标轴:

sysuse auto, clear
set scheme qlean

* 去除均值
sum price
replace price = price - r(mean)
sum weight
replace weight = weight - r(mean)

* 选择 rep78 作为分组变量
tab rep78

tw ///
sc price weight if rep78 == 1 || ///
sc price weight if rep78 == 2 || ///
sc price weight if rep78 == 3 || ///
sc price weight if rep78 == 4 || ///
sc price weight if rep78 == 5 ||, ///
leg(order(1 "rep78 == 1" 2 "rep78 == 2" ///
3 "rep78 == 3" 4 "rep78 == 4" ///
5 "rep78 == 5") pos(5) ring(0) ///
row(3)) || ///
pcarrowi 0 -2000 0 2500, color(gs10) ///
lw(*1.8) msize(2) mlw(*1.2) ///
barbsize(2) mstyle(ed_arrow) || ///
pcarrowi -10000 0 10000 0, color(gs10) ///
lw(*1.8) msize(2) mlw(*1.2) ///
barbsize(2) mstyle(ed_arrow) ///
text(5000 -1000 "II", size(*1.5)) ///
text(5000 1000 "I", size(*1.5)) ///
text(-5000 -1000 "III", size(*1.5)) ///
text(-5000 1000 "IV", size(*1.5)) ///
text(-400 -2000 "-2000", size(*0.8)) ///
text(-400 -1000 "-1000", size(*0.8)) ///
text(-400 0 "0", size(*0.8)) ///
text(-400 1000 "1000", size(*0.8)) ///
text(-400 2000 "2000", size(*0.8)) ///
text(10000 -200 "10000", size(*0.8)) ///
text(5000 -200 "5000", size(*0.8)) ///
text(0 -200 "0", size(*0.8)) ///
text(-5000 -200 "-5000", size(*0.8)) ///
text(-10000 -200 "-10000", size(*0.8)) ///
xsc(off range(-2000 2000)) ysc(off range(-8000 10000))

这样就绘制出来了:

图三

关于箭头的设置有个很有意思的关系,就是 msize() 和 barbsize() 的关系,两个选项的关系可以使用下面的图表示:

图四

这幅图的绘制代码如下:

clear all
local cmd
forval x = 1/7{
forval y = 0/`x'{
local x1 = `x' + 0.5
local cmd (pcarrowi `y' `x' `y' `x1', msize(`x') barbsize(`y')) `cmd'
}
}
di "`cmd'"
gr tw `cmd', yla(0(1)8, ang(0)) xla(1(1)8) leg(off) yti("barbsize()") xti("msize()") plotr(color(white))

前面的绘图代码里我选择的是 msize == barbsize。

点击这里跳转到 RStata 短书平台获取附件:如何使用 Stata 绘制四象限坐标图?

评论