0
点赞
收藏
分享

微信扫一扫

R语言ggplot2包绘制散点图详解


  • List item
    R语言的ggplot包可以实现各种复杂的制图功能,本文以散点图为例,介绍ggplot2代码的使用方法。
    首先,使用R内置数据attitude绘制complaints和learning的散点图。请注意ggplot2语法和R原生代码的区别。ggplot2采用图层模式,不同图层用“+”叠加。

> head(attitude,3)
rating complaints privileges learning raises critical advance
1 43 51 30 39 61 92 45
2 63 64 51 54 63 73 47
3 71 70 68 69 76 86 48

首先用ggplot()函数指定数据源,之后使用geom_point()函数绘制散点图,该函数使用mapping参数传入x和y所在的列。

ggplot(data = attitude) + 
geom_point(mapping = aes(x = complaints, y = learning))

R语言ggplot2包绘制散点图详解_r语言

那么,如何对散点进行分类呢?我们采用CO2数据。

> head(CO2)
Grouped Data: uptake ~ conc | Plant
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2

这次在aes中指定了color属性,设置为Plant列,这样可以对不同的Plant对应的散点应用不同的颜色。

ggplot(data = CO2) + 
geom_point(mapping = aes(x = conc, y = uptake, color = Plant))

R语言ggplot2包绘制散点图详解_ggplot2_02

同样,还可以指定size参数,使散点大小与某一参数相关。

ggplot(data = CO2) + 
geom_point(mapping = aes(x = conc, y = uptake, color = Plant, size=conc))

R语言ggplot2包绘制散点图详解_自定义_03

指定shape参数,使散点的形状与某一参数相关。

ggplot(data = CO2) + 
geom_point(mapping = aes(x = conc, y = uptake, color = Plant, size=conc, shape=Type))

R语言ggplot2包绘制散点图详解_自定义_04

下面使用iris数据展示ggplot2的更多功能。

> head(iris,3)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa

在mapping的aes参数中,还可以指定透明度alpha。

ggplot(data = iris) + 
geom_point(mapping = aes(x = Sepal.Length, y = Sepal.Width, alpha= Petal.Length , color = Species, shape= Species, size= Petal.Width))

R语言ggplot2包绘制散点图详解_数据_05

以上这些图像都采取了默认的设置,如果我们想自定义散点的形状、颜色等参数时,该怎么办呢?

R语言ggplot2包绘制散点图详解_ggplot2_06

引入color参数,可以自行设置颜色;使用shape参数可以自定义形状,各形状对应的序号如下。注意到color等参数是与mapping参数并列的。

ggplot(data = iris) + 
geom_point(mapping = aes(x = Sepal.Length, y = Sepal.Width), color = "red",shape=11)

ggplot(data = iris) + 
geom_point(mapping = aes(x = Sepal.Length, y = Sepal.Width, color= Species), color = c("#0FC62A","orange","#ABC"))

R语言ggplot2包绘制散点图详解_随机噪声_07

2.3.3.2. 多图布局
如何将不同分类的变量绘制到不同的图上,实现多图布局呢?
在geom_point函数后用“+”连接facet_wrap()函数,其中首个参数为用于分类的变量前加“~”,nrow参数表示每行布局的图像数。

ggplot(data = iris) + 
geom_point(mapping = aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape= Species, size= Petal.Length)) +
facet_wrap(~ Species, nrow = 2)

R语言ggplot2包绘制散点图详解_r语言_08

如果需要用两个变量实现多图布局,可使用facet_grid()函数指定行列对应的变量,用“~”分隔。

ggplot(data = CO2) + 
geom_point(mapping = aes(x = conc, y = uptake, color=Plant)) +
facet_grid(Type ~ Treatment)

R语言ggplot2包绘制散点图详解_自定义_09

若“~”前后的参数换为“.”,则只在列或行进行多图布局。

ggplot(data = CO2) + 
geom_point(mapping = aes(x = conc, y = uptake, color=Plant)) +
facet_grid(Type ~ .)
ggplot(data = CO2) +
geom_point(mapping = aes(x = conc, y = uptake, color=Plant)) +
facet_grid(. ~ Treatment)

散点图常常会出现点重叠的情况,尤其是数据四舍五入后作图。通过调整参数position = “jitter”,可以避免这种网格化,为每个点添加少量随机噪声。因为没有两个点可能会接收到相同数量的随机噪声,所以这就使避免了散点堆积的情况。

ggplot(data = CO2) + 
geom_point(mapping = aes(x = conc, y = uptake, color=Plant), , position = "jitter") +
facet_grid(Type ~ Treatment)

R语言ggplot2包绘制散点图详解_自定义_10

主要参考文献:# ​​R for Data Science​​​​https://r4ds.had.co.nz/data-visualisation.html​​


举报

相关推荐

0 条评论