温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
作者: 王鹏 (西安交通大学/香港城市大学)
邮箱: patrick.wang@my.cityu.edu.hk
Source: Jann B. Customizing Stata graphs made easy (part 2)[J]. The Stata Journal, 2018, 18(4): 786-802. -Link-
目录
Stata 绘图很灵活,但是对 Stata 图形外观的自定义却比较麻烦。主要原因是决定图形默认外观的设置都存储于外部文件,也就是方案文件 (scheme files) 中,而这些方案文件使用了独特的程序语言。因此,我们在用 Stata 制图时,都是简单采用 s2color
这一默认设置,但这并不总是最佳选择。为此,本文引入了 grstyle set
命令,让 Stata 图形的颜色设定变的更简单。
在开始之前,需要先安装两个命令。
ssc install grstyle, replace
ssc install palettes, replace
首先,我们需要使用 grstyle init
来初始化默认格式设置,然后选择一个或多个 grstyle set
的子命令进行的设定。为了避免冲突,后期进行的设置会自动覆盖前期进行的设置。
grstyle set
有设置背景、图例、颜色、置信区间的多个子命令,其语句格式如下:
grstyle set subcommand [arguments] [, options] : [elements]
grstyle set plain
会为图形设置一个朴素的外观,即白色背景、无阴影标签、黑色标题、灰色的网格线,其语句为:
grstyle set plain [, horizontal compact [no]grid minor dotted noextend box]
horizontal
选项会使得 y 轴的标签横排显示,默认设置为竖排;grid
选项使所有的横向、纵向以及最大最小值的网格线显示出来;nogrid
去除所有网格线;box
选项会在图形区域外绘制边框。
set scheme sj //将默认图形方案设置为Stata Journal所使用的方案,默认为s2color(factory setting)
grstyle init //初始化
grstyle set plain, horizontal grid box //设置背景和坐标系
sysuse auto, clear
scatter price mpg, by(foreign) //以变量foreign分组,画出散点图
grstyle set legend
可以设置图例的位置以及样式,其语句为:
grstyle set legend [clockposstyle] [, nobox stacked inside klength(textsizestyle)
clockposstyle
设定图例的位置 (时钟位置,取值为 0 到 12 的整数,具体可查看 help clockposstyle
);nobox
去除图例的边框(包括背景颜色);inside
将图例放置在绘图区域内部。
set scheme sj
grstyle init
grstyle set plain, horizontal grid
grstyle set legend 2, inside //将图例放置于绘图区域内部的2点钟位置(右上)
sysuse auto, clear
separate price, by(foreign) shortlabel //根据foreign变量将price生成两个变量
scatter price0 price1 mpg //绘制散点图
grstyle set color
命令可以分配颜色,其语句为:
grstyle set color [argument] [, plots(numlist) palette options] [: elements]
其中,[argument]
为 palette [[, palette options] / [ palette [, palette options] / ... ]]
。
palette
是调色板选项,可以选择多种预先设定的调色板,具体可使用help colorpalette
进行查看。palette options
是调色板选项,例如 n()
设定调色板的大小,即颜色数量;reverse
使调色板中颜色的顺序反转;opacity
设置透明度。
Note:若出现错误「colrspace_library_palettes.sthlp not found」,则需要安装 ssc install colrspace, replace
。
set scheme sj
grstyle init
grstyle set plain, horizontal
grstyle set legend 3, nobox
grstyle set color economist //使用economist调色板,可使用命令colorpalette economist查看调色板颜色
sysuse nlsw88, clear //导入数据
graph bar wage if occ<9, over(occ) asyvars over(union) //根据union变量分组,绘制柱状图
grstyle set color ptol, rainbow n(8) reverse //使用彩虹调色板,反转颜色顺序,设定颜色数量为8种
graph bar wage if occ<9, over(occ) asyvars over(union)
grstyle set color Greens, n(8) //使用绿色调色板,设定颜色数量为8种
graph bar wage if occ<9, over(occ) asyvars over(union)
grstyle init
grstyle set imesh, horizontal compact minor //将背景设置为灰色网格样式
grstyle set legend 4, nobox stack
grstyle set color hue, n(4) : p#lineplot //使用hue调色板,p#lineplot设置线条颜色
sysuse uslifeexp, clear //导入数据
label variable le_wfemale "white females"
label variable le_wmale "white males"
label variable le_bfemale "black females"
label variable le_bmale "black males"
line le_wfemale le_wmale le_bfemale le_bmale year, lwidth(*1.5 ..) ytitle(Life expectancy) //绘制折线图,设置线宽
grstyle init
grstyle set plain
grstyle set color #286090 #449d44 #31b0d5 #ec971f #c9302c //自定义5种颜色
drop _all
input primary success info warning danger //录入数据
primary success info warning danger
12 14 2 8 5
end
graph pie primary success info warning danger, legend(off) plabel(_all name, size(*1.5) color(white)) //绘制饼图
grstyle set ci
可以改变置信区间颜色的透明度,使图形更加美观,其语句为:
grstyle set ci [colors] [, opacity(numlist) palette options]
colors
设置置信区间的颜色,默认为灰色;opacity()
设置不透明度,默认为50,完全透明为0,完全不透明为100。
set scheme s2color
grstyle init
grstyle set plain, horizontal grid
grstyle set color Dark2 //使用Dark2调色板,可使用命令colorpalette Dark2查看调色板颜色
grstyle set legend 10, inside nobox //设置图例位置为10点钟位置,置于绘图区域内部
grstyle set ci Dark2, opacity(20) //设置置信区间的不透明度为20%
sysuse auto, clear
twoway (scatter price length if foreign==0) (scatter price length if foreign==1) (lpolyci price length if foreign==0, clstyle(p1)) (lpolyci price length if foreign==1, clstyle(p2)), legend(order(2 "Foreign" 1 "Domestic"))
Note: 以上程序需要在 Stata15 以上实现。
分配标记符号和线条样式的语句为:
grstyle set symbol [palette] [, plots(numlist) palette options] [: elements]
grstyle set lpattern [palette] [, plots(numlist) palette options] [: elements]
Stata 的默认色彩设置中,所有图形都采用实心圆圈或实线,这使得图形在黑白打印的出版物中难以辨别。为了提高图形的可读性,可以使用该命令更改符号和线条的形状。
set scheme s2color
grstyle init
grstyle set plain
grstyle set color Set1 //使用Set1调色板,可使用命令colorpalette Set1查看调色板颜色
grstyle set symbol
grstyle set lpattern
sysuse auto, clear
separate price, by(foreign) shortlabel
two connect price0 price1 weight, ytitle(Price) sort
grstyle set symbol T S //设置符号格式,T代表三角形,S代表正方形,具体可通过help symbolstyle查看
grstyle set lpattern "-." "--.." //设置线条格式,具体线条格式可通过help linepatternstyle查看
grstyle set color white, p(1 2) : p#markfill //设置填充颜色
two connect price0 price1 weight, ytitle(Price) sort
连享会-直播课 上线了!
http://lianxh.duanshu.com
免费公开课:
直击面板数据模型 - 连玉君,时长:1小时40分钟 Stata 33 讲 - 连玉君, 每讲 15 分钟. 部分直播课 课程资料下载 (PPT,dofiles等)
支持回看,所有课程可以随时购买观看。
专题 | 嘉宾 | 直播/回看视频 |
---|---|---|
⭐ 最新专题 ⭐ | DSGE, 因果推断, 空间计量等 | |
⭕ Stata数据清洗 | 游万海 | 直播, 2 小时,已上线 |
研究设计 | 连玉君 | 我的特斯拉-实证研究设计,-幻灯片- |
面板模型 | 连玉君 | 动态面板模型,-幻灯片- |
面板模型 | 连玉君 | 直击面板数据模型 [免费公开课,2小时] |
Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会学习群-常见问题解答汇总:
✨ https://gitee.com/arlionn/WD