Stata 绘制金字塔图

金字塔图是一种金字塔形状图表,因具有形象说明、便于理解的特点,经常应用于学术研究和市场分析中。今天我们来学习绘制一幅容易上手且实用的金字塔图,利用学习工作中的数据,也可以随心绘制这样的图表进行展示。

构造数据

首先构造数据:

clear
input input low high mean
1 1.6 2.2 1.9
2 1.5 2.3 1.9
5 2.9 0.01 1.9
3 1.0 2.9 1.9
4 0.5 3.2 1.9
end

添加变量标签:

label var input "投入"
label var low "低产出"
label var high "高产出"
label var mean "均值"

为 input 变量添加赋值标签:

label def inputlbl ///
1 "Annual Donations in U.S." ///
2 "Seroprevalence" ///
3 "Exposures of Transmission" ///
4 "Probability of Transmission" ///
5 "Sensitivity of Test"

label value input inputlbl

绘制图表

添加文本标签并调整文本标签的范围与颜色后,就能制作一幅完整的金字塔图了:

* 绘图
tw ///
rbar low mean input, hori || ///
rbar high mean input, hori ///
yla(, value) leg(pos(6) row(1) ///
order(1 "低产出" 2 "高产出")) ///
xti("Predicted Cases of Transfusio-Tansmission T. Cruzi") ///
ti("Sensitivity of Results to Inputs") ///
subti("绘制:微信公众号 RStata") ///
text(5 1 "99.99%", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(4 1.3 "0.8%", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(3 1.5 "1", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(5 2.4 "93.5%", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(4 2.6 "5%", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(3 2.5 "3", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(2 1.1 "1:28,600", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(1 1.2 "13.4 million", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(2 2.6 "1:18,200", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white)) ///
text(1 2.5 "19 million", box j(center) ///
margin(l+1 r+1 t+1 b+1) fc(white) lc(white))

* 保存图片
gr export pic.png, width(2400) replace

点击这里跳转到 RStata 短书平台获取附件:Stata 绘制金字塔图

评论