R实战 第五篇:绘图(ggplot2)

本文转载自:http://www.javaxxz.com/thread-359443-1-1.html

ggplot2包实现了基于语法的、连贯一致的创建图形的系统,由于ggplot2是基于语法创建图形的,这意味着,它由多个小组件构成,通过底层组件可以构造前所未有的图形。ggplot2可以把绘图拆分成多个面板,且能够按照顺序创建多重图形,基本上,无所不能,是R开发人员必学必会的包。
ggplot2图形系统的核心理念是:

把绘图与数据分离,把数据相关的绘图与数据无关的绘图分离;
按图层作图,有利于结构化思维;
具有命令式作图的调整函数,使绘图更具灵活性,绘制出来的图形美观,同时避免繁琐细节。

使用ggplot2包创建图形时,每个图形都是由函数ggplot()创建的,由几何对象来控制绘图,并增加了可读性更高的集合图形选项。
一,初始化图形
使用函数ggplot()初始化图形对象,并指定绘图图形的数据源和变量。

  1. ggplot(data = NULL, mapping = aes())
复制代码



参数data: 用于指定要用到的数据源;
参数mapping:使用aes()函数指定每个变量的角色,除x和y之外的其他参数,例如,size、color、shape等,必须采用name=value的形式,

  1. aes(x, y, ...)
复制代码



常见的图形美学选项是:

color:对点、线和填充区域的边界进行着色
fill:对填充区域着色
alpha:演示的透明度,从透明(0)到不透明(1)
linetype:图案的线条(1=实线、2=虚线、3=点、4=点破折号、5=长破折号、6=双破折号)
size:点的尺寸和线的宽度
shape:点的形状(和par()函数的pch参数相同)



position:绘制条形图和点等对象的位置
binwidth:直方图的宽度
notch:表示方块图是否应该有缺口
sides:地毯图的位置("b"=底部、"l"=左部、"r"=右部、"bl"=左下部,等)
width:箱线图的宽度
group:分组
stroke

在下面的小节中,使用数据集mtcars作为ggplot的输入:

  1. library(ggplot2)
  2. data("mtcars")
复制代码



二,几何对象(geometric)
函数ggplot()可以设置图形,但是没有视觉输出,需要使用一个或多个几何函数向图形中添加几何对象(geometric,简写为geom),包括点(point)、线(line)、条(bar)等,而添加几何图形的格式十分简单,通过符号“+”把几何图形添加到plot中:

  1. ggplot()+
  2. geom_xxx()
复制代码



例如,使用geom_point()函数输出点状图形,并接收以下美学参数:alpha、colour、fill、group、shape、size和stroke,

  1. ggplot(data=mtcars, aes(x=wt,y=mpg))+
  2.   geom_point(color="red",size=1,shape=0)
复制代码



在一个图形中,可以绘制多个几个几何图形

  1. ggplot(data=mtcars, aes(x=wt,y=mpg))+
  2.   geom_point(color="red",size=1,shape=0)+
  3.   geom_smooth(method="lm",color="green",linetype=2)
复制代码



三,主题
主题(Theme)用于控制图形的美学特征,ggplot图形的主题(theme)元素主要有三大类:布局(plot)、面板(panel)和坐标轴(axis),还有两个成分元素附着于面板上,主要包括两类:图例(legend)和带状区域(strip),ggplot2包通过theme()函数来统一控制图形的美学和文本特征,theme()函数的参数如下所示,详细文档请阅读:Modify components of a theme

  1. theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x,
  2.   axis.title.x.top, axis.title.y, axis.title.y.right, axis.text, axis.text.x,
  3.   axis.text.x.top, axis.text.y, axis.text.y.right, axis.ticks, axis.ticks.x,
  4.   axis.ticks.y, axis.ticks.length, axis.line, axis.line.x, axis.line.y,
  5.   legend.background, legend.margin, legend.spacing, legend.spacing.x,
  6.   legend.spacing.y, legend.key, legend.key.size, legend.key.height,
  7.   legend.key.width, legend.text, legend.text.align, legend.title,
  8.   legend.title.align, legend.position, legend.direction, legend.justification,
  9.   legend.box, legend.box.just, legend.box.margin, legend.box.background,
  10.   legend.box.spacing, panel.background, panel.border, panel.spacing,
  11.   panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major,
  12.   panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x,
  13.   panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.subtitle,
  14.   plot.caption, plot.margin, strip.background, strip.placement, strip.text,
  15.   strip.text.x, strip.text.y, strip.switch.pad.grid, strip.switch.pad.wrap, ...,
  16.   complete = FALSE, validate = TRUE)
复制代码



在使用ggplot2包绘制图形时,可以单独定义一个theme对象,然后添加到ggplot中,例如:

  1. mytheme <-theme(panel.grid.major.x = element_blank(),
  2.                   panel.grid.minor.x = element_blank(),
  3.                   panel.grid.major.y = element_line(color="lightblue",size =0.1,linetype="solid",lineend="round"),
  4.                   panel.grid.minor.y = element_blank(),
  5.                   panel.background = element_blank(),
  6.                   axis.ticks=element_blank()
  7. ggplot(data=mtcars, aes(x=wt,y=mpg))+
  8.   geom_point(color="red",size=1,shape=0)+
  9.   mytheme
复制代码



三,坐标(scale)
ggplot2包使用函数scale_x_xxx()和scale_y_xxx()分别控制坐标轴的数据,常用的函数有三个:scale_x_date,scale_x_discrete,scale_y_continuous,其中discrete表示坐标轴的值是可罗列的散列值,而continuous表示是不可罗列的连续值:

  1. scale_y_continuous(name = waiver(), breaks = waiver(),
  2.   minor_breaks = waiver(), labels = waiver(), limits = NULL,
  3.   expand = waiver(), oob = censor, na.value = NA_real_,
  4.   trans = "identity", position = "bottom", sec.axis = waiver())
复制代码



参数注释:

name:坐标轴上的文字
breaks:间距,如何对坐标轴的值进行间断,如果不设置,那么使用默认值,显示设置:NULL(不间断),数值向量,函数(输入是范围,输出是间距)
minor_breaks :同breaks参数,表示最小的间距
labels:标签,间距的文字显示;数量必须同breaks相同;
limits:数值向量,用于指定坐标轴的最小值和最大值 
expand:数值向量,默认值是c(0.05,0),用于指定数据离坐标轴的距离
oob:函数对象,Function that handles limits outside of the scale limits (out of bounds). 
na.value :用于替换缺失值的值
trans :指定转化对象,用于产生breaks和labels;
position:指定坐标轴的位置;
sec.axis:指定辅助坐标轴(secondary axis);

例如,x轴是以日期(date)为值的坐标轴,而y轴按照pretty_breaks()函数进行间断,标签按照特定的格式显示:

  1. library("scales")
  2. ggplot(data=mtcars, aes(x=wt,y=mpg))+
  3.   geom_point(color="red",size=1,shape=0)+
  4.   scale_x_date(date_labels="%Y-%m",date_breaks="1 month")+
  5.   scale_y_continuous(breaks=pretty_breaks(n=3), labels=scales::unit_format("k", 1e-3))
复制代码



在使用函数pretty_breaks()之前,请首先加载scales包;

  1. install.packages("scales")
复制代码



四,多重图
要同时绘制多个图形,可以使用gridExtra包中的grid.arrange()函数,绘制多重图时,需要定义一个ggplot()的列表,并把列表传递给grid.arrange()函数,在该函数中指定图形的布局(layout)例如:

  1. library("gridExtra")
  2. gs=list(NULL)
  3. gs[[1]]<-ggplot()
  4. gs[[2]]<-ggplot()
  5. gs[[n]]<-ggplot()
  6. grid.arrange(grobs=gs,ncol=1)
复制代码

 

猜你喜欢

转载自ilovejava1.iteye.com/blog/2413491
今日推荐