使用 Stata 绘制两个时期的变量取值对比图

今天给大家分享一个 Stata 图表绘制案例,原图是这样的:

下面就让我们用 Stata 来复现下这幅图。

首先设置工作目录和绘图主题:

cd "~/Desktop/使用 Stata 绘制两个时期的变量取值对比图/"
set scheme plotplain

plotplain 是 blindschemes 包的主题,可以使用 ssc install blindschemes 安装。

然后读入绘图数据:

use data-idHV3.dta, clear

由于数据的观测值顺序就是图表的纵轴顺序,所以我们根据这个顺序进行因子化:

gen id = _n
sencode Country, gen(country) gsort(-id)
drop id

然后我们生成三个辅助变量:

gen yl = -0.5
gen ymin = 0.8
gen ymax = 7.8

其中 yl 是“纵轴标签”的位置,这里之所以加上引号,是因为我们实际上是绘制了一层散点标签图层作为纵轴标签,而 Stata 中自带的纵轴标签没有左对齐的设置。

ymin 和 ymax 是为了绘制图中水平的网格线。

由于图上还有价格标签,所以我们再生成两个标签变量:

tostring _2000, gen(label1) force format(%6.1f)
replace label1 = "{bf:" + "$" + label1 + "}"

tostring _2022, gen(label2) force format(%6.1f)
replace label2 = "{bf:" + "$" + label2 + "}"

为了让标签水平居中,我们再把标签的纵坐标坐标微调下(因为使用了 hori 选项,所以横纵坐标转换了):

gen x = country + 0.15

然后就可以绘图了,首先绘制水平的网格线:

tw rspike ymin ymax country, hori lp(dot)

绘制箭头线:

tw rspike ymin ymax country, hori lp(dot) || ///
pcarrow _2000 country _2022 country, hori

由于 Stata 没有纵坐标轴标签左对齐的功能,所以手动绘制一列标签:

tw rspike ymin ymax country, hori lp(dot) || ///
pcarrow _2000 country _2022 country, hori || ///
sc country yl, m(i) mlab(country) mlabsize(*0.8)

再设置下 x 轴:

tw rspike ymin ymax country, hori lp(dot) || ///
pcarrow _2000 country _2022 country, hori || ///
sc country yl, m(i) mlab(country) mlabsize(*0.8) ///
xsc(noline) xla(1 "$1.0" 2 "$2.0" 3 "$3.0" ///
4 "$4.0" 5 "$5.0" 6 "$6.0" 7 "$7.0", noticks ///
glp(solid) glc(gs10) glw(*0.5) labsize(*0.7) ////
labcolor(gray))

设置 y 轴和取消图例:

tw rspike ymin ymax country, hori lp(dot) || ///
pcarrow _2000 country _2022 country, hori || ///
sc country yl, m(i) mlab(country) mlabsize(*0.8) ///
xsc(noline) xla(1 "$1.0" 2 "$2.0" 3 "$3.0" ///
4 "$4.0" 5 "$5.0" 6 "$6.0" 7 "$7.0", noticks ///
glp(solid) glc(gs10) glw(*0.5) labsize(*0.7) ////
labcolor(gray)) ///
yla(1(1)26, glp(dot) glc(gs10) glw(*0.8) value ///
nogrid) ///
ysc(off) leg(off)

添加标题、副标题、脚注之类的:

tw rspike ymin ymax country, hori lp(dot) || ///
pcarrow _2000 country _2022 country, hori || ///
sc country yl, m(i) mlab(country) mlabsize(*0.8) ///
xsc(noline) xla(1 "$1.0" 2 "$2.0" 3 "$3.0" ///
4 "$4.0" 5 "$5.0" 6 "$6.0" 7 "$7.0", noticks ///
glp(solid) glc(gs10) glw(*0.5) labsize(*0.7) ////
labcolor(gray)) ///
yla(1(1)26, glp(dot) glc(gs10) glw(*0.8) value ///
nogrid) ///
ysc(off) leg(off) ///
note(" Chart: James Wood Source: Kaggle Get the data Download image Created with Datawrapper", size(*0.6)) ///
ti(" {bf:BigMac Prices 2000 - 2022}", justification(left) bexpand) ///
subti(" Over the last 22 years the price of a BigMac has increase by an average of 100%, but with a few Commonwealth" " outliners seeing their burger prices inflate to between 200% and 300%... Ouch.", justification(left) bexpand size(*0.8))

最后我们在三个国家的线条上面再覆盖一层红色的线条来强调:

tw rspike ymin ymax country, hori lp(dot) || ///
pcarrow _2000 country _2022 country, hori || ///
sc country yl, m(i) mlab(country) mlabsize(*0.8) ///
xsc(noline) xla(1 "$1.0" 2 "$2.0" 3 "$3.0" ///
4 "$4.0" 5 "$5.0" 6 "$6.0" 7 "$7.0", noticks ///
glp(solid) glc(gs10) glw(*0.5) labsize(*0.7) ////
labcolor(gray)) ///
yla(1(1)26, glp(dot) glc(gs10) glw(*0.8) value ///
nogrid) ///
ysc(off) leg(off) ///
note(" Chart: James Wood Source: Kaggle Get the data Download image Created with Datawrapper", size(*0.6)) ///
ti(" {bf:BigMac Prices 2000 - 2022}", justification(left) bexpand) ///
subti(" Over the last 22 years the price of a BigMac has increase by an average of 100%, but with a few Commonwealth" " outliners seeing their burger prices inflate to between 200% and 300%... Ouch.", justification(left) bexpand size(*0.8)) || ///
sc x _2000, m(i) mlab(label1) mlabpos(9) mlabsize(*0.8) mlabc(gray) || ///
sc x _2022, m(i) mlab(label2) mlabpos(3) mlabsize(*0.8) mlabc(gray) || ///
sc x _2000 if inlist(Country, "Canada", "Australia", "New Zealand"), m(i) ///
mlab(label1) mlabpos(9) mlabsize(*0.8) mlabc(red) || ///
sc x _2022 if inlist(Country, "Canada", "Australia", "New Zealand"), m(i) ///
mlab(label2) mlabpos(3) mlabsize(*0.8) mlabc(red) || ///
pcarrow _2000 country _2022 country if inlist(Country, "Canada", "Australia", "New Zealand"), hori color(red)

最后一步我把字体设置成了 Roboto。

然后这个图我们就复现好啦!

点击这里跳转到 RStata 短书平台获取附件:使用 Stata 绘制两个时期的变量取值对比图

评论