资源描述
关于 ggplot2 包的几个例子library(ggplot2)# ggplot2 例子1:图层控制与直方图# 建立数据层p - ggplot(mpg, aes(x = hwy)# 建立直方图图层p - p + geom_histogram(position = identity, # 分布类型:单独的,非累积 alpha = 0.5,#透明度# 按照年份填充颜色, density 为使用密度函数aes(y = .density.,fill = factor(year)# 曲线类型# 分布类型:单独的,非累积# 颜色按年份# 建立密度函数分布图层p - p + stat_density(geom = line, position = identity, aes(colour = factor(year)# 根据某个变量分开绘制p - p + facet_grid(drv year)# drv 为行变量, year 为列变量# ggplot2 例子 2 :位置调整与条形图# 条形图各种形式with(mpg,table(class,year)p - ggplot(data = mpg, aes(x = class,fill = factor(year)p + geom_bar(position = dodge)# 将不同年份的数据并列放置p + geom_bar(position = stack)# 将不同年份数据推叠放置p + geom_bar(position = fill)# 和 stack 类似,但是以百分比的形式出现p + geom_bar(position = identity,alpha = 0.5)# 直接显示,要显示成透明才能看到# 美国 GDP 增长率例子y = c(1.1,1.8,2.5,3.6,3.1,2.7,1.9,-0.1,-3.5,3.0)x = 2001:2010data = data.frame(x,y) # ggplot2 一定要是数据框的形式p - ggplot(data, aes(x,y,fill = y)# 建立数据层,颜色以 y 变量实现渐变p - p + geom_bar(stat = identity) # 图形类型为独立显示p - p + geom_abline(intercept = 0, slope = 0,size=1,colour=gray)# 设置 y 坐标的范围# 设置坐标标签# 设置标题p - p + geom_text(aes(label=y),hjust=0.5, vjust=-0.5 ) # 添加标签,设置偏移 系数p - p + scale_y_continuous(limits=c(-3.8,4.2)p - p + labs(x= 年份 , y=GDP 增长率%)p - p + opts(title = 美国 GDP 增长率)# 课间佐料:同一个窗口画多个图layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(1,1), c(1,1), TRUE)plot(x1, y1)plot(x2, y2)plot(x3, y3)# ggplo2 例子 3 :散点图p - ggplot(mpg, aes(cty, hwy)# 对色彩的形状的控制p1 - p + geom_point(aes(colour = factor(year), # 颜色按年区分shape = factor(year), size = displ),alpha = 0.6, position = jitter)# 点状按年区分# 点的大小以变量displ 控制# 在点太过集中的时候,错开# 控制透明度print(p1) # 对坐标的控制cty.mean=with(mpg,mean(cty)cty.sd=with(mpg,sd(cty)p1 + scale_x_continuous(trans=log, # 对 x 轴进行对数变换# 只需要在x 轴显示坐标breaks=c(cty.mean-cty.sd,cty.mean,cty.mean+cty.sd),# 只需要在x 轴显示坐标对应的命名labels=c(high, mean, low)+ scale_y_continuous(trans=log)# 对 y 轴进行对数变换# 添加文字说明p - ggplot(mpg, aes(x=cty, y=hwy,colour=factor(year),label=year)p - p + geom_text(hjust=0,vjust=-1,alpha=0.8)p - p + geom_point(size=3,aes(shape=factor(year)# 矩阵散点图plotmatrix(USArrests) + geom_smooth() # ggplot2 包自带的矩阵散点图函数pairs(USArrests)# 基础包自带的矩阵散点图# ggplo2 例子4 :时间序列library(quantmod)getSymbols(ASSEC,src=yahoo,from = 1997-01-01)close - (Cl(SSEC)time - index(close)value - as.vector(close)p - ggplot(data.frame(time,value),aes(time,value)p - p + geom_line()yrng - range(value)xrng - range(time)data - data.frame(start=as.Date(c(1997-01-01,2003-01-01),end=as.Date(c(2002-12-30,2012-01-20), core=c(jiang,hu)timepoint -as.Date(c(1999-07-02,2001-07-26,2005-04-29,2008-01-10,2010-03-31)events - c(证券法实施,国有股持减,股权分置改革,次贷危机爆发,融资融券 试点 )data2 - data.frame(timepoint,events,stock=valuetime %in% timepoint)p - p + geom_line() if (F) p - p + geom_rect(aes(NULL,NULL,xmin = start, xmax = end, fill = core), ymin = yrng1, ymax=yrng2, data = data)p - p + scale_fill_manual(values = alpha(c(blue,red),0.2)# 添加文本说明p - p + geom_text(aes(timepoint, stock, label = events), data = data2, vjust = -2, size = 5)# 添加标记点p - p + geom_point(aes(timepoint, stock),data = data2,size = 5,colour = red,alpha = 0.5)
展开阅读全文