# Stata绘图：柱状图专题-T212

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

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

E-Mail: yangx94@qq.com

Source: 世界银行 stata-visual-library

## 1. 多变量水平柱状图（Horizontal bar with multiple variables）

``````global graph_opts ///
title(, justification(left) ///
color(black) span position(11)) ///
graphregion(color(white)) ///
ylabel(,angle(0) nogrid) ///
xtitle(,placement(left) justification(left)) ///
yscale(noline) xscale(noline) ///
legend(region(lcolor(none) fcolor(none)))

global  pct `" 0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%" "'

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-better.dta" , clear

betterbar ///
??_correct  checklist  refer med_any  med_class_any_6 med_class_any_16 ///
, ///
\${graph_opts} ///
over(facility_type) ///
xlabel(\${pct}) ///
barlab(mean) ///
legend(rows(1) symxsize(small) symysize(small))
``````

``````global  graph_opts                             ///定义暂元
title(, justification(left)                  ///
color(black) span position(11))              ///前两行为图标题设置
graphregion(color(white))                    ///图片背景颜色
ylabel(,angle(0) nogrid)                     ///Y轴标签设置
xtitle(,placement(left) justification(left)) ///x轴标题设置
yscale(noline) xscale(noline)                ///X轴与Y轴刻度设置
legend(region(lcolor(none) fcolor(none)))            //图例颜色设置

global  pct `" 0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%" "'

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-better.dta" , clear

betterbar ///
??_correct  checklist  refer med_any ///
med_class_any_6 med_class_any_16 ///前两行为变量名
, ///
\${graph_opts}                    ///调用暂元
over(facility_type)              ///根据该变量分组
xlabel(\${pct})                   ///调用暂元
barlab(mean)                     ///画出各变量均值
legend(rows(1) symxsize(small) symysize(small)) //图例行数与图例标志长宽设置
``````

## 2. 两变量柱状图（Bar plot of two variables）

``````global graph_opts1 ///
bgcolor(white) ///
graphregion(color(white)) ///
legend(region(lc(none) fc(none))) ///
ylabel(,angle(0) nogrid) ///
title(, justification(left) color(black) span position(11)) ///
subtitle(, justification(left) color(black))

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-over.dta" , clear

graph bar treat_correct ///
, ///
over(type) ///
asyvars ///
bargap(20) ///
over(study) ///
over(case) ///
nofill ///
blabel(bar, format(%9.2f)) ///
\${graph_opts1} ///
bar(1 , lcolor(black) lwidth(thin) fintensity(100)) ///
bar(2 , lcolor(black) lwidth(thin) fintensity(100)) ///
legend(rows(1) ///
order(0 "Measurement:" 1 "Standardized Patient" 2 "Clinical Vignette")) ///
ytitle("Providers ordering correct treatment {&rarr}", ///
placement(bottom) ///
justification(left)) ///
ylabel(\${pct})
``````

``````global graph_opts1 ///
bgcolor(white) ///
graphregion(color(white)) ///
legend(region(lc(none) fc(none))) ///
ylabel(,angle(0) nogrid) ///
title(, justification(left) color(black) span position(11)) ///
subtitle(, justification(left) color(black)) //子标题设置

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-over.dta" , clear

graph bar treat_correct ///
, ///
over(type)  ///type包括Standardized Patient与Vignette
asyvars     ///将第一个over()设置的变量作为Y轴变量
bargap(20)  ///柱之间的距离
over(study) ///study包括Bihar、China与Delhi
over(case)  ///case包括Diarrhea (ORS)与Tuberculosis (AFB or CXR)
nofill      ///忽略缺少的分类
blabel(bar, format(%9.2f)) ///柱上端的数字设置
\${graph_opts1} ///
bar(1 , lcolor(black) lwidth(thin) fintensity(100)) ///柱格式设置
bar(2 , lcolor(black) lwidth(thin) fintensity(100)) ///
legend(rows(1) ///
order(0 "Measurement:" 1 "Standardized Patient" 2 "Clinical Vignette")) ///
ytitle("Providers ordering correct treatment {&rarr}", ///
placement(bottom) ///
justification(left)) ///
ylabel(\${pct})
``````

## 3. 两轴组合柱状图（Combined bar plots with two axes）

``````use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-two-axes.dta", clear

* Adjust variable for bar position
gen x = int1mo+.5

* Create individual graphs
* ------------------------
foreach foodGroup in animal fruit grain veg starch processed_sugar {

if "`foodGroup'" == "animal"  local graphTitle Animal Sourced
if "`foodGroup'" == "fruit"   local graphTitle Fruit
if "`foodGroup'" == "grain"   local graphTitle Grain
if "`foodGroup'" == "veg"     local graphTitle Vegetables
if "`foodGroup'" == "starch"  local graphTitle Starchy Foods
if "`foodGroup'" == "processed_sugar"  local graphTitle Processed/Sugar

twoway ///
bar number_group x if food_group=="`foodGroup'", ///
yaxis(1) ytitle("Avg. Number of Foods from" "Group Consumed Last Month", axis(1)) ///
barwidth(.9) fintensity(inten0) lcolor(black) ///
xlabel(0 "0" 3 "3" 6 "6" 9 "9" 12 "12") ///
ylabel(0 "0" 1 "1" 2 "2" 3 "3", axis(1)) || ///
line total_exp int1mo if food_group=="`foodGroup'", ///
yaxis(2) ytitle("Total Value of Exp." "1000 Real Tz Sh.", axis(2)) ///
ylabel(0 "0" 500 "500" 1000 "1000" 1500 "1500" 2000 "2000" 2500 "2500", axis(2)) ///
xlabel(3 "3" 6 "6" 9 "9" 12 "12") lwidth(1.2) ///
title("`graphTitle'") xtitle("Month of Interview") ///
graphregion(color(white)) bgcolor(white) ///
legend(off) ///
name("`foodGroup'")
}

* Combine graphs into one
* -----------------------
graph combine starch animal fruit grain processed_sugar veg, ///
graphregion(color(white)) plotregion(color(white))
``````

``````use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-two-axes.dta", clear

* Adjust variable for bar position
gen x = int1mo+.5

* Create individual graphs
* ------------------------
foreach foodGroup in animal fruit grain veg starch processed_sugar {

if "`foodGroup'" == "animal"  local graphTitle Animal Sourced
if "`foodGroup'" == "fruit"   local graphTitle Fruit
if "`foodGroup'" == "grain"   local graphTitle Grain
if "`foodGroup'" == "veg"     local graphTitle Vegetables
if "`foodGroup'" == "starch"  local graphTitle Starchy Foods
if "`foodGroup'" == "processed_sugar"  local graphTitle Processed/Sugar

twoway ///
bar number_group x if food_group=="`foodGroup'",    ///设置为柱状图
yaxis(1) ytitle("Avg. Number of Foods from" "Group Consumed Last Month", axis(1)) ///指左边Y轴
barwidth(.9) fintensity(inten0) lcolor(black)     ///
xlabel(0 "0" 3 "3" 6 "6" 9 "9" 12 "12")           ///
ylabel(0 "0" 1 "1" 2 "2" 3 "3", axis(1)) ||       ///不同Y轴图形设置之间用“||”隔开
line total_exp int1mo if food_group=="`foodGroup'", ///设置为折线图
yaxis(2) ytitle("Total Value of Exp." "1000 Real Tz Sh.", axis(2)) ///
ylabel(0 "0" 500 "500" 1000 "1000" 1500 "1500" 2000 "2000" 2500 "2500", axis(2)) ///指右边Y轴
xlabel(3 "3" 6 "6" 9 "9" 12 "12") lwidth(1.2)     ///
title("`graphTitle'") xtitle("Month of Interview")    ///整体图片设置
graphregion(color(white)) bgcolor(white)              ///
legend(off) ///
name("`foodGroup'")                                   //将图片进行临时存储，方便后续组合各图
}

* Combine graphs into one
* -----------------------
graph combine starch animal fruit grain processed_sugar veg, ///需组合的图片名
graphregion(color(white)) plotregion(color(white))
``````
• 组合图片如下：

## 4. 并排水平柱状图（Side by side horizontal bar plot）（基于命令betterbar）

``````global graph_opts ///
note(, justification(left) color(black) span position(7)) ///
title(, justification(left) color(black) span position(11)) ///
subtitle(, justification(left) color(black) span position(11)) ///
graphregion(color(white)) ///
ylabel(,angle(0) nogrid) ///
ytitle("") ///
xtitle(,placement(left) justification(left)) ///
yscale(noline) xscale(noline) xsize(7) ///
legend(region(lcolor(none) fcolor(none)))

local n_5 = 599
local n_6 = 601

local title_5 = "Classic case of presumed TB"
local title_6 = "TB case with positive sputum report"
quietly forvalues i = 5/6 {

local case = `i' - 4
use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-betterbar.dta", clear

gen n = 1
bysort med_generic: egen med_class_typ = mode(med_class), minmode // Label with  mosttypical medicine code
label values med_class_typ med_k

keep if case == `i'

labelcollapse  (firstnm) n med_class_typ med_generic_encoded sp_location, ///
by(med_generic facilitycode) ///
vallab(med_class_typ med_generic_encoded sp_location)

labelcollapse   (sum) n (firstnm) med_generic_encoded med_class_typ, ///
by(med_generic) ///
vallab(med_class_typ med_generic_encoded)

capture separate n, by(med_generic_encoded) shortlabel

foreach var of varlist n?* {

local theLabel : var label `var'
local theLabel = regexr("`theLabel'","med_generic_encoded == ","")

capture summarize n if med_generic == "`theLabel'"
capture local theN = `r(mean)'

label var `var' "`theLabel' [`theN']"
}

foreach var of varlist n?* {

replace `var' = . if `var' < 5 // Exclude low volumes
replace `var' = `var'/`n_`i'' // Number of interactions
quietly summarize `var'
if `r(N)' == 0 drop `var'
}

drop if med_generic == "Sodium Chloride" // not an active ingredient

betterbar (n?*) , ///
stat(sum) over(med_class_typ) by(med_class_typ) nobylabel nobycolor d(1)  ///
legend(span cols(1) position(3) ring(1) symxsize(small) symysize(small) size(small)  ///
dropzero ///
xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%") ///
ysize(6) labsize(2) ///
\${graph_opts} ///
title("Case `case' (N=`n_`i'')") subtitle("`title_`i''") ///
name(figure_4_`case')
}

grc1leg figure_4_1 figure_4_2 ///
, ///
position(3) ///
graphregion(color(white)) ///
xsize(7)
``````

• note()，注释设置

• subtitle()，副标题设置

• by()与over()搭配使用，使图形能够基于分类清楚展示

• grc1leg，将多个图形合并为具有一个共同图例的图形

• 组合图片如下：

## 5. 两变量堆积柱状图（Stack bar graph by two variables）

``````global graph_opts1 ///
bgcolor(white) graphregion(color(white)) ///
legend(region(lcolor(none) fcolor(none))) ///
ylabel(,angle(0) nogrid) ///
subtitle(, justification(left) color(black) span position(11)) ///
title(, color(black) span)

global pct `" 0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%" "'

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-stack-by.dta", clear

graph bar med_b2_antister_cat?? ///
if dr_3 == 1 ///
, ///
stack over(checkgroup) nofill ///
ylabel(\${pct}) ///
legend(order(5 "No Medication" ///
4 "Antibiotic and Steroid" ///
3 "Antibiotic" ///
2 "Steroid" ///
1 "No Antibiotic or Steroid") ///
cols(1) position(3) ///
symxsize(small) symysize(small) size(small)) ///
\${graph_opts1} ///
bar(5, color(white) lcolor(black) lpattern(solid) lwidth(thin)) ///
bar(1,lwidth(thin) lcolor(black)) bar(2,lwidth(thin) lcolor(black)) ///
bar(3,lwidth(thin) lcolor(black)) bar(4,lwidth(thin) lcolor(black)) ///
subtitle("Referral", color(black) justification(center) position(12)) ///
name(figure_1)

graph bar med_b2_antister_cat?? ///
if dr_3 == 0 ///
, ///
stack over(checkgroup) nofill ///
ylabel(\${pct}) ///
legend(order(5 "No Medication" ///
4 "Antibiotic and Steroid" ///
3 "Antibiotic" ///
2 "Steroid" ///
1 "No Antibiotic or Steroid") ///
cols(1) position(3) ///
symxsize(small) symysize(small) size(small)) ///
\${graph_opts1} ///
bar(5, color(white) lcolor(black) lpattern(solid) lwidth(thin)) ///
bar(1,lwidth(thin) lcolor(black)) ///
bar(2,lwidth(thin) lcolor(black)) ///
bar(3,lwidth(thin) lcolor(black)) ///
bar(4,lwidth(thin) lcolor(black)) ///
subtitle("No Referral", color(black) justification(center) position(12)) ///
name(figure_2)

grc1leg figure_2 figure_1 ///
, ///
position(3) ///
graphregion(color(white)) ///
xsize(7) ///
rows(1) ///
legendfrom(figure_2)
``````

• stack，表明把变量纵着堆积

• grc1leg，rows()，表明两个图的放置位置

• grc1leg，legendfrom()，表明使用哪个图的图例

• 组合图片如下：

## 6. 并排水平柱状图（Side by side horizontal bar plot）（基于命令weightab）

``````global graph_opts ///
title(, justification(left) color(black) span position(11)) ///
graphregion(color(white) lcolor(white) lwidth(med) lalign(center)) ///
ylabel(,angle(0) nogrid) xtitle(,placement(left) justification(left)) ///
yscale(noline) xscale(noline) legend(region(lcolor(none) fcolor(none)))

local opts lwidth(thin) lcolor(white) lalign(center)

label define  case 1 "Case 1" 2 "Case 2" 3 "Case 3" 4 "Case 4" , modify

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-weightab.dta" , clear

weightab ///
correct treat_cxr re_3 re_4 treat_refer t_12 ///
med_any med_l_any_1 med_l_any_2 med_l_any_3  med_k_any_9   ///
if city == 2 ///
[pweight = weight_city] ///
, ///
\${graph_opts} ///
barlab ///
barlook(1 `opts' fintensity(100)) ///
title("Patna") ///
over(case) ///
graph ///
legend(off) ///
xlabel(\${pct}) ///
name(Fig_1_1)

weightab ///
correct treat_cxr re_3 re_4 treat_refer t_12 ///
med_any med_l_any_1 med_l_any_2 med_l_any_3  med_k_any_9  ///
if city == 3 ///
[pweight = weight_city] ///
, ///
\${graph_opts} ///
barlab ///
barlook(1 `opts'  fintensity(100)) ///
title("Mumbai") ///
over(case) ///
graph ///
legend(position(5) ring(0) cols(1) symxsize(small) symysize(small)) ///
xlabel(\${pct}) ///
name(Fig_1_2)

graph combine Fig_1_1 Fig_1_2 ///
, ///
\${comb_opts} ///
xsize(7) r(1)
``````

• barlook()，柱格式设置

• graph，这个选项是命令weightab画图的必选项

• 组合图片如下：

## 7. 带有变量分组、标准误差条和跨组比较的水平柱状图（Horizontal bar plot with grouping of variables, standard error bars, and cross-group comparisons）

``````global pct `" 0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%" "'

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-better-ci.dta", clear

betterbar ///
(dr_3 correct_treatment)  ///
(med_b2_any_antibiotic med_b2_any_steroid med_b2_any_antister med_l_any_2 med_b2_any_schedule_h med_b2_any_schedule_h1 med_b2_any_schedule_x med_l_any_1)   ///
, ///
over(city) xlabel(\$pct) se bin ///
legend(position(5) ring(0) cols(1) symxsize(small) symysize(small)) ///
ysize(7) n barlab(upper)

``````

• se，表示添加标准误差条
• n，表示在图例中添加每组观测值数量
• barlab()，表示数值的显示位置

## 8. 横向堆积柱状图（Horizontal stack bar plot）

``````global graph_opts1 ///
title(, justification(left) color(black) span position(11)) ///
graphregion(color(white) lcolor(white) lalign(center)) ///
ylabwl(,angle(0) nogrid)  ///
yscale(noline) legend(region(lcolor(none) fcolor(none)))

global pct `" 0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%" "'

use "https://gitee.com/arlionn/stata-visual-library/raw/master/Library/data/bar-stack-cat.dta" , clear

local x = 0
quietly foreach var of varlist ///
correct treat_cxr re_3 re_4 treat_refer ///
med_any med_l_any_1 med_l_any_2 med_l_any_3 med_k_any_9 {

mean `var' [pweight = weight_city]
mat a = e(b)
local mean = a[1,1]
local mean = string(round(100*`mean',0))
local mean = substr("`mean'",1,strpos("`mean'",".")+1)

local ++x
local theLabel : var label `var'
local theLabels `" `theLabels' `x' "`theLabel'" "' // [`mean'%]

capture mat drop theResult
reg `var' i.city [pweight = weight_city]
local theR21 = `e(r2)'
mat theResult = nullmat(theResult) , [`theR21']

reg `var' i.city i.case [pweight = weight_city]
local theR22 = `e(r2)' - `theR21'
mat theResult = nullmat(theResult) , [`theR22']

reg `var' i.city i.case i.type_formal  [pweight = weight_city]
local theR23 = `e(r2)' - `theR21' - `theR22'
mat theResult = nullmat(theResult) , [`theR23']

reg `var' i.city i.case i.type_formal i.sp_city_id  [pweight = weight_city]
local theR24 = `e(r2)' - `theR21' - `theR22' - `theR23'
mat theResult = nullmat(theResult) , [`theR24']

reg `var' i.city i.case i.type_formal i.sp_city_id i.sp_city_mbbs [pweight = weight_city]
local theR25 = `e(r2)' - `theR21' - `theR22' - `theR23' - `theR24'
mat theResult = nullmat(theResult) , [`theR25']

mean `var' [pweight = weight_city]
mat a = e(b)
local mean = a[1,1]
mat theResult = nullmat(theResult) , [`mean']

mat theResults = nullmat(theResults) \ theResult
matlist theResults
}

clear
svmat theResults
gen n = _n
label define n `theLabels'
label values n n

graph bar (sum) theResults1 theResults2 theResults3 theResults4 theResults5  ///
, ///
ylabel(\$pct) ///
\${graph_opts1} ///
horizontal stack over(n) xsize(6) ///
bar(1, lcolor(black) lwidth(thin)) ///
bar(2, lcolor(black) lwidth(thin)) ///
bar(3, lcolor(black) lwidth(thin)) ///
bar(4, fcolor(black) lcolor(black) lwidth(thin)) ///
bar(5, fcolor(gs12) lcolor(black) lwidth(thin)) ///
legend(position(5) ring(0) cols(1) symxsize(small) symysize(small)  ///
order(6 "Variance Explained By:" ///
1 "City Setting" ///
2 "Case Scenario" ///
3 "MBBS Degree" ///
4 "All SP Characteristics" ///
5 "Full Interaction Model"))
``````

• horizontal，表示图形方向设置为横向

## 9. 相关推文

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

`ssc install lianxh, replace`

## 相关课程

### 最新课程-直播课

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

### 关于我们

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

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

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`