ggplot2
一.基本概念
"一张统计图形就是从数据到几何对象(geometric object, 缩写为geom, 包括点、线、条形等)的图形属性(aesthetic attributes, 缩写为aes, 包括颜色、形状、大小等)的一个映射。此外, 图形中还可能包含数据的统计变换(statistical transformation, 缩写为stats), 最后绘制在某个特定的坐标系(coordinate system, 缩写为coord)中, 而分面(facet, 指将绘图窗口划分为若干个子窗口)则可以用来生成数据中不同子集的图形。" 源自ggplot2的作者[Hadley Wickham]。
1.数据data和映射mapping
将数据中的变量映射到图形属性,映射控制了两者之间关系。

2.标度scale
标度负责控制映射后图形属性的显示方式。具体形式上来看是图例和坐标刻度。 Scale和Mapping是紧密相关的概念。

3.几何对象Geom
几何对象代表我们在图中实际看到的图形元素,如点、线、多边形等。

4.统计变换Stat
对原始数据进行某种计算,例如对二元散点图加上一条回归线。

5.坐标系统Coord
坐标系统控制坐标轴幵影响所有图形元素,坐标轴可以进行变换以满足不同的需要。

6.图层Layer
数据、映射、几何对象、统计变换等构成一个图层。图层可以允许用户一步步的构建图形,方便单独对图层进行修改。

7.分面Facet
条件绘图,将数据按某种方式分组,然后分别绘图。分面就是控制分组绘图的方法和排列形式。

8.主题theme
9.存储和输出
二.简单示例
1)散点图
1.数据
在ggplot2中, 所接受的数据集必须为数据框(data.frame)格式
library(ggplot2)
str(mpg)
head(mpg)


2.映射
aes()函数是ggplot2中的映射函数, 所谓的映射即为数据集中的数据关联到相应的图形属性过程中一种对应关系
每个点都有自己图像上的属性,比如x坐标,y坐标,点的大小、颜色和形状,这些都叫做aesthetics,即图像上可观测到的属性,通过aes函数来赋值
p <- ggplot(data=mpg, mapping=aes(x=cty, y=hwy))
p

summary(p)

3.几何对象
通过”+”以图层的方式加入点的几何对象
p + geom_point()

summary(p + geom_point())


注1:2、3步可合并为
ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()
注2:改变横纵坐标名称
ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point() + xlab("A") + ylab("B")

注3:geom_point()中参数的用法
#shape
ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point(shape=17)


#size
ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point(size=2.5)

#color
ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point(color="red")

4.将年份映射到颜色/形状/大小属性(按年份分组)
将分组变量(因子或字符变量,可通过factor()强制性将非因子变量转为因子变量)赋值给颜色或形状或大小属性(变量赋值必须在aes里面),实现分组散点图的绘制。
①颜色
ggplot(mpg,aes(x=cty, y=hwy, colour=factor(year)))+geom_point()

②大小
ggplot(mpg,aes(x=cty, y=hwy, size=factor(year)))+geom_point()

③形状
ggplot(mpg,aes(x=cty, y=hwy, shape=factor(year)))+geom_point()

5.增加平滑曲线
geom_smooth可以增加一条线,而stat_smooth是可以添加一个置信区间范围的(利用方差值)。
①stat_smooth
ggplot函数中赋予的值是全局性质的,如果不希望全局生效,放到后面+对应的图层中去。
如:
CASE1:stat_smooth对不同颜色的点,各自拟合曲线
ggplot(mpg,aes(x=cty, y=hwy, colour=factor(year)))+geom_point()+stat_smooth()

CASE2:stat_smooth对所有点,拟合一条曲线
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year)))+stat_smooth()
#等价形式
ggplot() +geom_point(data=mpg, aes(x=cty, y=hwy, colour=factor(year)))+stat_smooth(data=mpg, aes(x=cty, y=hwy))

②geom_smooth
geom_smooth():这个函数就是为散点图添加一条平滑的曲线(包含直线),它有个参数method, 指定曲线平滑方法,可选"lm", "glm", "gam", "loess", "rlm",默认会使用"loess"。
ggplot(mpg,aes(x=cty, y=hwy))+ geom_point(aes(colour=factor(year))) + geom_smooth(method = lm)
#等价形式
ggplot(mpg,aes(x=cty, y=hwy))+ geom_point(aes(colour=factor(year))) + stst_smooth(method = lm)

6.自定义颜色/形状
scale_color_brewer()或者scale_color_manual()函数自定义点的颜色;scale_shape_manual()函数自定义点的形状;
下面我们将分组变量year同时赋值给颜色和形状属性。
①改变颜色
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year)))+stat_smooth(method = lm)+scale_color_manual(values =c('blue','red'))

②改变颜色,通过Accent(调色板)
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year)))+stat_smooth(method = lm)+scale_color_brewer(palette = "Accent")

③改变形状
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(shape=factor(year)))+stat_smooth(method = lm)+scale_shape_manual(values = c(2, 9))

④同时改变颜色和形状
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),shape=factor(year)))+stat_smooth(method = lm)+scale_color_brewer(palette = "Accent")+scale_shape_manual(values = c(2, 9))

7.将连续型变量(排量)映射到颜色/大小属性
①大小
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(size=displ))+stat_smooth()

②大小,指定我们size的变化范围,即控制点的绝对大小的范围,默认点的大小为1-6ms,可更改为2-8ms
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(size=displ))+stat_smooth()+scale_size_continuous(range = c(2, 8))

③大小,同时将年份按颜色分组
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+stat_smooth()

④颜色
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(color=displ))+stat_smooth()

⑤颜色(通过人为设置色阶)
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(color=displ))+stat_smooth()+ scale_color_gradient(low="lightblue", high="darkblue")

8.分面facet
分面设置在ggplot2应该也是要经常用到的一项画图内容,在数据对比以及分类显示上有着极为重要的作用。
facet_wrap和facet_grid不同在于facet_wrap是基于一个因子进行设置,facets表示形式为:变量(单元格)
而facet_grid是基于两个因子进行设置,facets表示形式为:变量变量(行列),如果把一个因子用点表示,也可以达到facet_wrap的效果,也可以用加号设置成两个以上变量
例如:变量+变量~变量 的形式,表示对三个变量设置分面。
①facet_wrap
facet_wrap(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, as.table = TRUE, drop = TRUE)
下面对参数进行说明:
· nrow,ncol : 分面所设置成的行和列,参数为数值,表示几行或者几列
· scales : 参数fixed表示固定坐标轴刻度,free表示反馈坐标轴刻度,也可以单独设置成free_x或free_y(把scales 设置成free之后,可以看出每个分面都有自己的坐标刻度,当然我们也可以单独对x轴或y轴设置)
· shrink : 也和坐标轴刻度有关,如果为TRUE(默认值)则按统计后的数据调整刻度范围,否则按统计前的数据设定坐标
· drop : 表示是否去掉没有数据的分组,默认情况下不显示,逻辑值为FALSE
CASE1:
#利用facet分别显示不同年份的数据
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(colour=class,size=displ))+ stat_smooth()+scale_size_continuous(range = c(2, 8))+facet_wrap(~ year, ncol=1)

facet_wrap()是关键,facet与wrap两个词组合,就是逐面地包起来。里面我们选择按照year这个变量来分层,就可以将1999与2008分开。一定要注意!这里在year前面有个~,回归中的用法。而最后的ncol = 1代表着我们的小窗口是1列,指定了1列之后,默认就是两行(因为年份一共只有两种)。
colour = class。class也是数据集中的一个变量,代表不同种类的汽车,而因为汽车的种类非常多,所以颜色也就变的很多。
CASE2:
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(colour=class,size=displ))+ stat_smooth()+scale_size_continuous(range = c(2, 8))+facet_wrap(~ year)

如果不加ncol=1,会默认横着排列
CASE3:
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(colour=class,size=displ))+ stat_smooth()+scale_size_continuous(range = c(2, 8))+facet_wrap(~ year,nrow=1)

或者想要指定几行,则使用nrow = 1
②facet_grid
facet_grid(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, drop = TRUE)
下面对参数进行说明:
· scales 、shrink、drop 同上
· as.table :和小图排列顺序有关的选项。如果为TRUE(默认)则按表格方式排列,即最大值(指分组level值)排在表格最后即右下角,否则排在左上角
· margins :通过TRUE或者FALSE表示否设置而一个总和的分面变量,默认情况为FALSE,即不设置
· space :表示分面空间是否可以按数据进行缩放,参数和scales一样
CASE1:
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(colour=class,size=displ))+ stat_smooth()+scale_size_continuous(range = c(2, 8))+facet_grid(.~ year)

一行多列
CASE2:
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(colour=class,size=displ))+ stat_smooth()+scale_size_continuous(range = c(2, 8))+facet_grid(year~ .)

一列多行
9.其他调整
①更改透明度alpha
当数据量非常大时,会导致数据重叠点非常严重,可通过使用半透明的点避免。alpha参数控制点的透明度。
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(size=2,color="red",alpha=0.005))

②用坐标控制图形显示范围xlim,ylim
ggplot(mpg,aes(x=cty, y=hwy))+geom_point(aes(size=2,color="red",alpha=0.005))+coord_cartesian(xlim = c(15, 25),ylim=c(15,30))

③调整坐标轴coord
CASE1:横纵坐标交换coord_flip()
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+stat_smooth()

ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+stat_smooth()+ coord_flip()

CASE2:转换为极坐标系coord_polar()
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+stat_smooth()+ coord_polar()

④去除掉拟合曲线的置信区间 se=FALSE
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+stat_smooth(se=FALSE)

⑤增加标题 opts
CASE1:使用ggtitle
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+ggtitle("汽车油耗与型号")

CASE2:使用ggtitle,并居中hjust
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+ggtitle("汽车油耗与型号")+theme(plot.title = element_text(hjust = 0.5))

hjust 其实就是左右移动的意思,0.5
CASE3:使用labs
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+labs(title="汽车油耗与型号")

⑥增加标题横纵坐标名称 labs
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+labs(y='每加仑高速公路行驶距离',x='每加仑城市公路行驶距离')

⑦改变图例位置 theme
决定放哪儿的参数是:theme()中的legend.position,默认状态theme(legend.position = "right"),然后我们可以通过“right”, “left”, “top”, “bottom”,把他放在上/下/左,或者去掉“none”
ggplot(mpg,aes(x=cty,y=hwy))+geom_point(aes(colour=factor(year),size=displ))+theme(legend.position = "left")

⑧改变图例名称、大小guides
ggplot(mpg, aes(x=cty, y=hwy))+ geom_point(aes(colour=class,size=displ))+ guides(size=guide_legend(title='排量'),colour = guide_legend(title='车型',override.aes=list(size=5)))

⑨去掉背景theme
ggplot(mpg, aes(x=cty, y=hwy))+ geom_point(aes(colour=class,size=displ))

ggplot(mpg, aes(x=cty, y=hwy))+ geom_point(aes(colour=class,size=displ))+theme(panel.background=element_blank())

⑩共用同一坐标轴,作对比
ggplot(mpg, aes(x=cty))+geom_point(aes(y=hwy,colour="red"))+geom_point(aes(y=displ,colour="green"))

等价形式
ggplot()+geom_point(aes(x=mpg$cty,y=mpg$hwy),color="red")+geom_point(aes(x=mpg$cty,y=mpg$displ),color="green")

2)玫瑰图/鸡冠花图
绘制 Coxcomb Plots (鸡冠花图,又名玫瑰图),width的值可以调整各个扇形区域之间的距离。
ggplot(mpg, aes(x = factor(mpg$class)))+geom_bar(width = 0.7,aes(color=factor(mpg$class))) + coord_polar()

增添颜色
ggplot(mpg, aes(x = factor(mpg$class),fill=mpg$class))+geom_bar(width = 0.7) + coord_polar()
