tw rbar y1 y0 x, color("49 79 105") barwidth(0.8) hori lw(none) || /// rbar y2 y1 x, color("104 172 150") barwidth(0.8) hori lw(none) || /// rbar y3 y2 x, color("211 139 40") barwidth(0.8) hori lw(none) || /// sc x1 y0 if x > 7, m(i) mlab(label1) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x1 y1, m(i) mlab(label2) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x1 y2 if x < 16, m(i) mlab(label3) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x yl, m(i) mlab(country) mlabpos(3) mlabsize(*0.8) /// ysc(off) xsc(off) /// xla(-28(10)100, nogrid) /// yla(1(1)18, nogrid) /// note(`" The UN defines "Urban" differently for each country. To count as "urban", Japanese settlements need to have at least "' /// `" 50,000 inhabitants. In Iceland, 200 inhabitants are enough."', /// justification(left) size(*0.6))
这里再 note 前面添加空格是为了调整脚注和 “y 轴标签对齐”。
然后是设置图例:
tw rbar y1 y0 x, color("49 79 105") barwidth(0.8) hori lw(none) || /// rbar y2 y1 x, color("104 172 150") barwidth(0.8) hori lw(none) || /// rbar y3 y2 x, color("211 139 40") barwidth(0.8) hori lw(none) || /// sc x1 y0 if x > 7, m(i) mlab(label1) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x1 y1, m(i) mlab(label2) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x1 y2 if x < 16, m(i) mlab(label3) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x yl, m(i) mlab(country) mlabpos(3) mlabsize(*0.8) /// ysc(off) xsc(off) /// xla(-28(10)100, nogrid) /// yla(1(1)18, nogrid) /// note(`" The UN defines "Urban" differently for each country. To count as "urban", Japanese settlements need to have at least "' /// `" 50,000 inhabitants. In Iceland, 200 inhabitants are enough."', /// justification(left) size(*0.6)) /// leg(order(1 "Share of population that lives in the capital" /// 2 "in other urban areas" 3 "in rural areas") size(*0.8) /// row(1) pos(11) symysize(1.5pt) symxsize(1.5pt) colgap(0.5pt) /// alignment(middle) keygap(0.3pt))
最后再设置下 title、subtitle 和 caption:
tw rbar y1 y0 x, color("49 79 105") barwidth(0.8) hori lw(none) || /// rbar y2 y1 x, color("104 172 150") barwidth(0.8) hori lw(none) || /// rbar y3 y2 x, color("211 139 40") barwidth(0.8) hori lw(none) || /// sc x1 y0 if x > 7, m(i) mlab(label1) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x1 y1, m(i) mlab(label2) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x1 y2 if x < 16, m(i) mlab(label3) mlabpos(3) mlabsize(*0.7) mlabcolor("white") || /// sc x yl, m(i) mlab(country) mlabpos(3) mlabsize(*0.8) /// ysc(off) xsc(off) /// xla(-28(10)100, nogrid) /// yla(1(1)18, nogrid) /// note(`" The UN defines "Urban" differently for each country. To count as "urban", Japanese settlements need to have at least "' /// `" 50,000 inhabitants. In Iceland, 200 inhabitants are enough."', /// justification(left) size(*0.6)) /// leg(order(1 "Share of population that lives in the capital" /// 2 "in other urban areas" 3 "in rural areas") size(*0.8) /// row(1) pos(11) symysize(1.5pt) symxsize(1.5pt) colgap(0.5pt) /// alignment(middle) keygap(0.3pt)) /// ti(" Population by Urban/Rural Areas", justification(left) bexpand) /// subti(" Share of population that lives in the capital, in urban areas and in rural areas, of selected countries," " 2014.", justification(left) bexpand size(*0.8)) /// caption(`" {fontface "SourceHanSerifSC-Medium":绘制:微信公众号 RStata}"', size(*0.6)) gr export "pic.png", replace width(4800)
评论