【收藏】stata绘图之graph bar 从经典到高级
Stata具有优良的图形绘制功能,而条形图又称柱状图。主要用矩形的长度来表示相互独立的变量大小。本节主要为大家介绍一维柱状图与二维柱状图的绘制。
命令格式为
graph hbar yvars [if] [in] [weight] [, options]
菜单式操作为
graph bar draws vertical bar charts. In a vertical bar chart, the y axis is numerical, and the x axis is categorical.
即横轴表示种类类别,纵轴表示数量大小。
graph bar (mean) wage, over(smsa) over(married) over(collgrad)
title("Average Hourly Wage, 1988, Women Aged 34-46")
subtitle("by College Graduation, Marital Status,
and SMSA residence")
note("Source: 1988 data from NLS, U.S. Dept. of Labor,
Bureau of Labor Statistics")
结果为:
简单的操作命令为
graph hbar (mean) public private, over(country)
graph hbar (mean) public private, over(country) stack
完整的案例代码如下:
generate total = private + public
graph hbar (asis) public private,over(country, sort(total) descending) stack
title( "Spending on tertiary education as % of GDP,1999", span pos(11) )
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span)
结果为:
二维柱状图的代码帮助为
菜单式操作为
经典介绍案例:
sysuse sp500
list date close change in 1/5
twoway bar change date in 1/57
twoway bar change date in 1/57, barw(.6)
The useful thing about twoway bar is that it can be combined with other twoway plottypes:
sysuse pop2000, clear
list agegrp maletotal femtotal
replace maletotal = -maletotal/1e+6
replace femtotal = femtotal/1e+6
twoway bar maletotal agegrp, horizontal xvarlab(Males) ||
bar femtotal agegrp, horizontal xvarlab(Females) ||
ylabel(1(1)17, angle(horizontal) valuelabel labsize(*.8))
xtitle("Population in millions") ytitle("")
xlabel(-10 "10" -7.5 "7.5" -5 "5" -2.5 "2.5" 2.5 5 7.5 10)
legend(label(1 Males) label(2 Females))
title("US Male and Female Population by Age")
subtitle("Year 2000")
note("Source: U.S. Census Bureau, Census 2000, Tables 1, 2 and 3", span)
更多高级绘图及计量经济学和stata应用,尽在2017年8月4-7日stata课程。关于图形这部分章节内容,中心将从图形介绍以及图形的使用功能简介意义方面着手介绍,让大家知道什么情况下应该用什么图。
另外学会各种图形的帮助文件能够画出更多精美实用的图形。
参考资料:stata帮助文件
【Stata特训】2017年8月Stata暑期特训课程火热招生中
(课程将会把讲义,包括各模型附带的stata实现过程(do文档)全部开放给大家)
l 计量经济学服务中心 l
更懂计量,更懂你
回复关键词:微信群,进入中心科研社群
学术QQ群:593977756(500人大群)
品牌网站:www.aiwanning.com