8.3 几种常用的图
8.3.1 散点图与连线图
指定图形中散点的表示符号:msymbol()
圆圈 菱形 正方形 三角形 +号 X号 小点
大、实心 O D S T + X 大、空心 Oh Dh Sh Th
小、实心 o d s t x p 小、空心 oh dh sh th
任务9.6,绘制万科A每年的负债率(杠杆水平)的散点图。
use \桌面\\d.dta\, clear scatter d year if dm==2, msymbol(Oh)
指定散点的颜色
scatter d year if dm==2, msymbol(d) mfcolor(green) mlcolor(red)
任务:绘制出万科A杠杆水平d和公司规模大小size的散点图,并标年度 scatter d size if dm==2, mlabel(year)
单独标出某年
use \桌面\\d.dta\, clear
twoway (scatter d size if dm==2&year==2004, mlabel(year)) (scatter d size if dm==2), legend(off)
指定图形中散点的连接方式:connect()
. 不连接
l 用直线连接
L 按X 在数据库中的顺序用直线连接 m 用直线连接中位值 s 用三次平滑曲线连接 J 以阶梯式直线连接 || 连接垂直方向上的两个点 II 在顶及底部添加短横线
对每种线条类型,可单独定义线条样式,如 c(l)为实线相连,c(l[-])以虚线相连 l 实线 l[_] 长线段 l[-] 中等长线段 l[.] 短线段 l[#] 空格
8.3.2条形图
任务9.7:使用浦发银行(股票代码为600000)的2007年-2008年8月股价数据ret1.dta绘制以下条形图,其中tclose 为收盘价,topen 为开盘价,tdate 为交易日,year为年份,month为月份,date为日期,high为当日最高股价,low为当日最低股价。
最原始的命令
use \桌面\\ret1.dta\
gsort year month list tclose year month
以上命令用于查看数据,从数据中可以看出,样本中包含两年的数据2007年和2008年,每年有12个月,每个月中有若干天的收盘价格,绘图时采用的为每个月的平均收盘价。 graph bar tclose over(year) over(month)
上图非常难看,横坐标的标示糊成了一团,nofill 命令将分类变量中缺失值忽略掉(在前 面将其做为一组来处理)
graph bar tclose over(year) over(month) nofill
上面的图中,各个条形图之间间距较大,仍然不够美观。而下面的命令选项asyvars 将使得 第一个分组变量变换成相应的若干y 变量。如2007,2008 graph bar tclose over(year) over(month) nofil asyvars
下面的命令对坐标轴的刻度、标识及标题进行调整 graph bar tclose over(year) over(month) nofil asyvars /// ytitle(月平均股价) /// ylabel(0(15)60) /// b1title(月份)
下面的命令美化图例
graph bar tclose over(year) over(month) nofil asyvars /// ytitle(月平均股价) ///
ylabel(0(15)60) /// b1title(月份)
legend(rows(1) position(1) ring(0))
graph bar tclose ,over(year) over(month label(angle(45))) /*将分组变量标识倾斜45度*/ /// nofill asyvars ytitle(月平均股价)
blabel(bar, format(%4.2f)) //给每个条形图加上数据并规定数据格式
8.3.3 直方图
任务9.8 绘制身高直方图,录入如下数据,将数据分为10 组,统计落入每组的频数,然后 绘制直方图。
*============================begin================================= clear
input x1-x10
142.3 156.6 142.7 145.7 138.2 141.6 142.5 130.5 134.5 148.8