# 史上最牛Stata绘图模版-schemepack：酷似R中的ggplot2-袁子晴

Stata 连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

## 1. 问题背景

<,,,, ,,, , ,,,,,,,,,,,, , , , , , , , , , >

## 2. 安装和使用

``````ssc install schemepack, replace
``````

``````set scheme white_tableau
``````

``````set scheme white_tableau, perm
``````

``````twoway (scatter var2 date if group==1), scheme(white_tableau)
``````
Stata设定绘图模版代码展开
``````set scheme swift_red
set scheme neon
set scheme rainbow
set scheme white_tableau
set scheme black_tableau
set scheme gg_tableau
set scheme white_cividis
set scheme black_cividis
set scheme gg_cividis
set scheme white_viridis
set scheme black_viridis
set scheme gg_viridis
set scheme white_hue
set scheme black_hue
set scheme gg_hue
set scheme white_brbg
set scheme black_brbg
set scheme gg_brbg
set scheme white_piyg
set scheme black_piyg
set scheme gg_piyg
set scheme white_ptol
set scheme black_ptol
set scheme gg_ptol
set scheme white_jet
set scheme black_jet
set scheme gg_jet
set scheme white_w3d
set scheme black_w3d
set scheme gg_w3d
``````

## 3. 实例演示

### 3.1 散点图

``````*** Scatter plot
twoway ///
(scatter var2 date if group==1) ///
(scatter var2 date if group==2) ///
(scatter var2 date if group==3) ///
(scatter var2 date if group==4) ///
(scatter var2 date if group==5) ///
(scatter var2 date if group==6) ///
(scatter var2 date if group==7) ///
(scatter var2 date if group==8) ///
(scatter var2 date if group==9) ///
(scatter var2 date if group==10) ///
(scatter var2 date if group==11) ///
(scatter var2 date if group==12) ///
, ///
legend(order(1 "group1" 2 "group2" 3 "group3"  4 "group4"  5 "group5" 6 "group6" 7 "group7" 8 "group8" 9 "group9" 10 "group10" 11 "group11" 12 "group12")) ///
title("Scatter plot") ///
note("By the Stata Guide")
``````

### 3.2 折线图

``````*** Line graph

twoway ///
(line var2 date if group==1) ///
(line var2 date if group==2) ///
(line var2 date if group==3) ///
(line var2 date if group==4) ///
(line var2 date if group==5) ///
(line var2 date if group==6) ///
(line var2 date if group==7) ///
(line var2 date if group==8) ///
(line var2 date if group==9) ///
(line var2 date if group==10) ///
(line var2 date if group==11) ///
(line var2 date if group==12) ///
, ///
legend(order(1 "group1" 2 "group2" 3 "group3"  4 "group4"  5 "group5" 6 "group6" 7 "group7" 8 "group8" 9 "group9" 10 "group10" 11 "group11" 12 "group12")) ///
title("Line plot") ///
note("The Stata Guide", size(vsmall))
``````

### 3.3 饼图

``````*** Pie chart

graph pie var2 if group <= 10, ///
over(group) plabel(_all percent, format(%9.2f)) ///
line(lcolor(black) lwidth(vvthin)) 	///                  // outline colors have to be manually added
title("Pie plot") ///
note("The Stata Guide", size(vsmall))
``````

### 3.4 箱线图

``````*** Box plot

graph box ///
var* ///
, ///
title("Box plot") ///
note("The Stata Guide", size(vsmall))
``````

### 3.5 柱状图

``````*** Histogram

histogram var4, percent ///
title("Histogram") ///
note("The Stata Guide", size(vsmall))
``````

### 3.6 竖向条形图

``````*** Bar graph

graph bar ///
var* ///
, ///
blabel(bar, format(%9.2f)) ///
title("Bar graph") ///
note("The Stata Guide", size(vsmall))
``````

### 3.7 横向条形图

``````*** Horizontal bar graph

graph hbar (mean) ///
var* ///
if group <= 6, ///
over(group) ///
percentages stack	///
legend(order(1 "Var 1" 2 "Var 2" 3 "Var 3"  4 "Var 4"  5 "Var 5" 6 "Var 6")) ///
title("Bar graph") ///
note("The Stata Guide", size(vsmall))
``````

### 3.8 置信区间带

``````*** Confidence bands

twoway ///
(lpolyci var1 var9, fcolor(%80)) ///
(lpolyci var2 var9, fcolor(%80)) ///
(lpolyci var3 var9, fcolor(%80)) ///
, ///
title("Confidence Interval") ///
note("The Stata Guide", size(vsmall))
``````

### 3.9 范围区间图

``````*** Range graphs

twoway ///
(rcapsym var2 var3 date if group==1, sort) ///
(rcapsym var2 var3 date if group==2, sort) ///
, ///
title("Range plots") ///
note("The Stata Guide", size(vsmall))
``````

### 3.10 面积图

``````*** Area graphs

twoway ///
(area den1d den1x, fcolor(%50)) ///
(area gen2d gen2x, fcolor(%50)) ///
(area gen3d gen3x, fcolor(%50)), ///
title("Density plots") ///
note("The Stata Guide", size(vsmall))
``````

### 3.11 附加标签的散点图

``````*** Scatter labels

twoway ///
(scatter var2 var1, mlabel(group)) ///
if date==22320 ///
, ///
title("Confidence Interval") ///
note("The Stata Guide", size(vsmall))
``````

### 3.12 分组散点图

``````*** By graphs

twoway ///
(scatter var2 var1) ///
if group <= 12, ///
by(group, yrescale xrescale)	///
by(, title("By graphs") note("The Stata Guide", size(vsmall)))
``````

## 5. 相关推文

Note：产生如下推文列表的 Stata 命令为：
`lianxh 绘图`

`ssc install lianxh, replace`

## 相关课程

### 最新课程-直播课

• Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

### 关于我们

• Stata 连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。
• 连享会-主页知乎专栏，700+ 推文，实证分析不再抓狂。直播间 有很多视频课程，可以随时观看。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会-常见问题解答：
https://gitee.com/lianxh/Course/wikis

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`