1.什么是qplot
qplot的意思是quick plot(快速作图),利用qplot我们可以非常方便地绘制各种图形。有时在其他作图系统中需要好几行代码才能搞定的事情,在qplot()中只需要一行代码就可以搞定。
通过?qplot()
命令查看qplot()的帮助文档:
使用方法:
qplot(x, y = NULL, ..., data, facets = NULL, margins = FALSE,
geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "",
main = NULL, xlab = deparse(substitute(x)),
ylab = deparse(substitute(y)), asp = NA, stat = NULL, position = NULL)
参数:
x, y :定义x轴和y轴
data:制定作图的数据集
facets:分面
margins:
xlim,ylim:设置x轴和y轴的显示区间,如xlim=c(0,20),ylim=c(-0.9,-0.5)
geom:定义几何对象
log:指定哪一个坐标轴应该取对数,log="x"表明x轴取对数,log="xy"说明xy同时取对数
main:图形的主标题,放置在图形顶端中部,以大号字体显示。如main="plot tittle"
xlab,ylab:设置x轴和y轴的标签文字
2.数据集准备
使用ggplot2中自带的一个叫做diamonds的数据集,里面包含了月5400颗钻石的价格和质量信息。示例如下:

为了方便示例,随机截取diamonds中的100个样本:
set.seed(1410) #使每次产生的随机数相同
dsmall <- diamonds[sample(nrow(diamonds), 100),]
3.一个简单的例子:绘制一张散点图
在Rstudio中输入下面的代码,画一张以carat为x轴price为y轴的散点图:
qplot(carat, price, data = diamonds)

qplot支持将变量的函数作为参数,下面画一张log(carat)对log(price)的图:
qplot(log(carat), log(price), data = diamonds)

画一张重量对体积(近似以xyz表示)的图:
qplot(carat, x*y*z, data = diamonds)

4.图形属性
在qplot中可以设置图形属性,以更形象地展示图形中的信息,常用的图形属性包括形状(shape)、颜色(colour)、大小(size)、透明度(alpha)等。
qplot(carat, price, data = dsmall) #不设置图形属性
qplot(carat, price, data = dsmall, colour = color) #设置color的颜色属性
qplot(carat, price, data = dsmall, shape = cut) #设置cut的形状属性
qplot(carat, price, data = dsmall, colour = color,shape = cut) #设置color的颜色属性,同时设置设置cut的形状属性
不设置图形属性:

设置color的颜色属性:

设置cut的形状属性:

设置color的颜色属性,同时设置设置cut的形状属性:

图形属性的手动设置
在上面的例子中,图形属性都是采用ggplot2的默认值,其实,我们可以通过I()
自己设定图形属性。
qplot(carat, price, data = dsmall, colour = I("red"))#手动设置图形的颜色属性

#手动设置图形的透明度属性,分母越小,透明度越大,通过设置透明度可以查看数据密集的区域
qplot(carat, price, data = diamonds, alpha = I(1/10))
qplot(carat, price, data = diamonds, alpha = I(1/100))
qplot(carat, price, data = diamonds, alpha = I(1/200))
设置alpha为1/10:

设置alpha为1/100:

设置alpha为1/200:

5.几何对象
在ggplot2中,几何对象描述了应该用何种对象对数据进行展示,其中一些几何对象还关联了相应的统计变换。通过改变几何对象,我们几乎可以画出任何形式的图。
下面展示一些常用的几何对象:
展示二维的变量关系:
- geom = "point":用于绘制散点图,当我们指定x轴和y轴后,ggplot2默认绘制散点图
- geom = "smooth":你和一条平滑曲线,并将曲线和标准误展示在图中
- geom = "boxplot":绘制箱线图,用于展示一系列点的分布情况
- geom = "jitter":绘制扰动图,用于展示一系列点的分布情况
- geom = "path"和geom = "line":在数据的点之间绘制连线,线条图(line)只能从创建从左到右的连线,而路径图(path)根据点在统计表格中的顺序进行连线。
展示一维的变量关系:
对于一维分布,几何对象的选择是根据变量的类型而指定的。
- 对于连续变量:geom = "histogram"绘制直方图,geom = "freqpoly"绘制频率多边形,geom = "density"绘制密度曲线。如果不指定几何对象,ggplot默认绘制直方图。
- 对于离散变量:geom = "bar"绘制条形图。
向点图中添加平滑曲线
用途:如果散点图中有非常多的数据点,在展示数据时,数据的变化趋势可能并不明显,这时我们可以在图中添加一条平滑曲线,用来更清楚地展示数据的变化趋势。
#添加平滑曲线
qplot(carat, price, data = diamonds, geom = c("point","smooth"))
qplot(carat, price, data = dsmall, geom = c("point","smooth"))
qplot(carat, price, data = dsmall, geom = c("point","smooth"),se = FALSE) #不使用标准误
作图效果依次如下:
绘制diamonds的平滑曲线:

绘制dsmall的平滑曲线:

绘制dsmall的平滑曲线,并去掉标准差:

不同平滑器的选择:
在拟合平滑曲线时,可以通过method来选择不同的平滑器。
- 当数据量较小时(<1000),默认method = "loess",使用局部回归的方法绘制平滑曲线。可通过span参数控制平滑度,取值范围从0(很不平滑)到1(很平滑)。
- 当数据量大于1000时,默认使用method = "gam"拟合平滑线。
箱线图和扰动图
用途:如果一个数据集中包含了一个分类变量和一个或多个连续变量,可以通过箱线图和扰动图显示连续变量是如何随着分类变量水平的变化而变化的。
#使用箱线图和扰动图显示钻石的价格如何随着颜色的变化而变化
qplot(color, price/carat, data = diamonds, geom = "boxplot") #箱线图
qplot(color, price/carat, data = diamonds, geom = "jitter") #扰动图
箱线图:

扰动图:

通过改变扰动图的透明度,展示数据点集中的部分:
qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/5)) #扰动图,透明度为1/5
qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/50)) #扰动图,透明度为1/50
qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/200)) #扰动图,透明度为1/200
透明度为1/5:

透明度为1/50:

透明度为1/200:

箱线图和扰动图的比较
- 扰动图可将所有的点绘制到图中,重叠比较严重,可通过调节透明度部分解决重叠问题。可调控size、colour、shape等图形属性。
- 箱线图使用数据的五个统计量(最小值,第一四分位数、中位数、第三四分位数和最大值)对对数据进行概括,可参考的统计值更多。可用colour控制外框线的颜色,用fill设置填充颜色,用size调节线的粗细。
直方图和密度图
用途:直方图和密度曲线图可以展示单个变量的分布,相对于箱线图而言,它们提供了更多的关于单个分布的信息,但不容易在不同组之间进行比较。
qplot(carat, data = diamonds, geom = "histogram") #直方图
qplot(carat, data = diamonds, geom = "density") #密度曲线图
直方图:

密度曲线图:

平滑度的调整:
- 直方图:通过binwidth调节组距,来调节平滑度。
- 密度曲线图:使用adjust参数控制曲线的平滑度,adjust取值越大,曲线越平滑。
#直方图,binwidth=1
qplot(carat, data = diamonds, geom = "histogram", binwidth = 1)
#直方图,binwidth=0.1
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1)
#直方图,binwidth=0.01
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01)
直方图,binwidth=1

直方图,binwidth=0.1

直方图,binwidth=0.01

#密度曲线图, adjust = 1
qplot(carat, data = diamonds, geom = "density", adjust = 1)
#密度曲线图, adjust = 0.1
qplot(carat, data = diamonds, geom = "density", adjust = 0.1)
#密度曲线图, adjust = 0.01
qplot(carat, data = diamonds, geom = "density", adjust = 0.01)
密度曲线图, adjust = 1:

密度曲线图, adjust = 0.1:

密度曲线图, adjust = 0.01:

要在不同组之间进行分布对比,只需要再添加一个图形映射即可:
#直方图,binwidth=1,通过填充颜色比较钻石颜色分组
qplot(carat, data = diamonds, geom = "histogram", binwidth = 1, fill = color)
#密度曲线图, adjust = 1,通过线条颜色比较钻石颜色分组
qplot(carat, data = diamonds, geom = "density", adjust = 1, colour = color)
直方图,binwidth=1,通过填充颜色比较钻石颜色分组:

密度曲线图, adjust = 1,通过线条颜色比较钻石颜色分组:

条形图
用途:使用条形图统计并展示每一个水平下的观测数量。在离散型变量的情况下,条形图和直方图呈现的结果相似。
#绘制钻石颜色的条形图
qplot(color, data = diamonds, geom = "bar")

时间序列中的线条图和路径图
用途:线条图和路径图通常用于可视化时间序列数据。线条图将点从左向右依次连接,而路径图按照点在数据集中出现的顺序进行连接。
下面使用ggplot2中自带的economics数据集进行演示,它包含了美国40年的经济数据。
#绘制失业率(unemploy/pop)和时间变化关系的线条图
qplot(date, unemploy/pop, data = economics, geom = "line")

#绘制失业率(unemploy/pop)和失业星期中位数(uempmed)变化关系的路径图
qplot(uempmed, unemploy/pop, data = economics, geom = "path")
#绘制失业率(unemploy/pop)和失业星期中位数(uempmed)变化关系的路径图,加上点
qplot(uempmed, unemploy/pop, data = economics, geom = c("point", "path"))
#绘制失业率(unemploy/pop)和失业星期中位数(uempmed)变化关系的路径图,加上点,年份映射上颜色
qplot(uempmed, unemploy/pop, data = economics, geom = c("point", "path"), colour = date)
绘制失业率(unemploy/pop)和失业星期中位数(uempmed)变化关系的路径图:

绘制失业率(unemploy/pop)和失业星期中位数(uempmed)变化关系的路径图,加上点:

绘制失业率(unemploy/pop)和失业星期中位数(uempmed)变化关系的路径图,加上点,年份映射上颜色:

分面
在前面的例子中,我们已经介绍过,可以使用图形属性(颜色和形状)来比较不同的分组,可以将所有的组绘制在一张图中。此外,分面也可以实现相同的功能,它将数据分隔成若干子集,然后创建一个图形的矩阵,将每一个子集绘制到图形矩阵的窗格中;所有子图采用相同的图形类型,并进行了一定的设计,使得他们之间可以进行方便的比较。
#以carat为x轴,并以color创建分面,将频数映射的y轴
qplot(carat, data = diamonds, facets = color ~.,
geom = "histogram", binwidth = 0.1, xlim = c(0,3))
#以carat为x轴,并以color创建分面,将密度映射的y轴
qplot(carat, ..density.., data = diamonds, facets = color ~.,
geom = "histogram", binwidth = 0.1, xlim = c(0,3))
以carat为x轴,并以color创建分面,将频数映射的y轴:

以carat为x轴,并以color创建分面,将密度映射的y轴:

网友评论