Stata 默认的绘图主题是很丑的,这是公认的。
那么能否使用 Stata 快捷的绘制出精美的图表呢?肯定是可以的。
为了方便大家选择,我搜集了几乎所有的 Stata 绘图主题,一共是 127 种。从 RStata 平台上搜索绘图主题即可找到本文相关的附件,里面有这些主题的安装包,运行下面的代码即可安装:
clear all * 设定工作目录(附件所在文件夹) global wd = "~/Desktop/Stata绘图主题" cd $wd * 安装相关的绘图主题包 foreach i in "scheme-burd" "blindschemes" /// "brewschemeextras" "dubois" /// "feigenbaum" "g538schemes" "prioscheme" /// "qlean" "rstata_scheme" "scheme_cgd" /// "scheme_cgd_on_black" "scheme_rbn1mono" /// "scheme_s2clr_on_white" "scheme_scientific" /// "scheme_tufte" "scheme_virdis" "scheme-lean" /// "scheme-modern" "scheme-mrc" "scheme-pih" /// "scheme-tfl" "schemepack" "uncluttered" /// "vgsg3" "yalescheme"{ net install `i'.pkg, from("$wd/scheme/`i'") replace }
注意 “~/Desktop/Stata绘图主题” 是 “Stata绘图主题” 文件夹在我的电脑上的路径,大家需要把这个替换成自己电脑上的路径。
那么如何获取可用的主题列表呢:
cap log close log using "schemeslist.smcl" , replace smclgraph query , schemeslog close infix strL v 1-200 using schemeslist.smcl, clear keep if index (v, "{col 5}" )replace v = subinstr (v, "{res}" , "" , .)gen scheme = ustrregexs(1) if ustrregexm(v, "5\}(.*)" )split scheme, parse ("{col 20}" )keep scheme1ren scheme1 schemesave scheme, replace
然后我们就得到了一个 128 行的变量 v,每个观测值都是一种绘图主题:
list scheme in 1/10 *> +-------------------------+ *> | scheme | *> |-------------------------| *> 1. | uncluttered | *> 2. | uncluttered_publication | *> 3. | 538 | *> 4. | 538bw | *> 5. | 538w | *> |-------------------------| *> 6. | black_brbg | *> 7. | black_cividis | *> 8. | black_hue | *> 9. | black_jet | *> 10. | black_piyg | *> +-------------------------+
为了对不同主题的样式进行比较,我们统一使用这样的一幅图:
sysuse auto, clear tw histogram mpg, width(5) ysc(alt axis(1)) || line weight mpg, yaxis(2) ysc(alt axis(2)) sort name(a, replace ) scheme(lightrstata) sysuse auto, clear sum price, mean local mean = r (mean )qui kdensity price, gen (x h ) nodrawtw line h x || area h x if x < `mean' , name(b, replace ) scheme(lightrstata) tw sc price weight || lpolyci price weight, name(c, replace ) scheme(lightrstata) generate order = _nexpand 3bysort order : generate which = _ndrop if which == 1 & price > 5000drop if which == 2 & price > 10000label def which 1 "<= $5000" 2 "<= $10000" 3 "all" label val which which gr box mpg, over(which ) over(foreign) name(d , replace ) scheme(lightrstata) gr combine a b c d , rows(2) scheme(lightrstata) xsize(20) ysize(12)
PDF 文档:Stata 绘图主题合集 最后为了方便大家比较和查阅不同的主题,我使用 Tex 编写了一本精美的小册子:
大家如果方便打印的话可以把它打印出来放在桌边以便查阅。
点击这里跳转到 RStata 短书平台获取附件:Stata 绘图主题合集
评论