根据Stata的帮助文档,我罗列部分柱状图的绘制方案。
分组累积柱状图
数据格式要求:分组变量与几个待加总变量,比如GDP分为公共部分GDP和私营部门GDP。
use https://www.stata-press.com/data/r17/educ99gdp, clear
generate total = private + public
# delimit ;
graph hbar (asis) public private,
over(country, sort(total) descending) stack //根据国别分组,根据总GDP进行降序排序,采取堆叠的形式
title( "Spending on tertiary education as % of GDP, 1999",
span pos(11) )
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span)
;
# delimit cr
分组百分比柱状图
generate frac = private/(private + public)
# delimit ;
graph hbar (asis) public private,
over(country, sort(frac) descending) stack percent //和上一个相比仅增加了percent选项
title("Public and private spending on tertiary education, 1999",
span pos(11) )
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span)
;
# delimit cr
单一分组均值图
数据结构:分组变量、多个呈现变量。比如统计城市的6和7月降雨,那么要分别生成变量6月和7月降雨。
use https://www.stata-press.com/data/r17/citytemp, clear
graph bar (mean) tempjuly tempjan, over(region)
多维分组均值图
数据结构:多个分组变量、多个呈现变量。比如统计城市的6和7月降雨,那么要分别生成变量6月和7月降雨。
graph bar tempjuly tempjan, over(division) over(region) nofill //两个分组变量
bargap(-30)
ytitle("Degrees Fahrenheit")
legend( label(1 "July") label(2 "January") )
title("Average July and January temperatures")
subtitle("by region and division of the United States")
note("Source: U.S. Census Bureau, U.S. Dept. of Commerce")
注意其中分类轴的标题名使用 b1title选项进行定义。