R語(yǔ)言的ggplot包可以實(shí)現(xiàn)各種復(fù)雜的制圖功能,本文以散點(diǎn)圖為例蟀俊,介紹ggplot2代碼的使用方法钦铺。
首先,使用R內(nèi)置數(shù)據(jù)attitude繪制complaints和learning的散點(diǎn)圖肢预。請(qǐng)注意ggplot2語(yǔ)法和R原生代碼的區(qū)別矛洞。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()函數(shù)指定數(shù)據(jù)源缚甩,之后使用geom_point()函數(shù)繪制散點(diǎn)圖谱净,該函數(shù)使用mapping參數(shù)傳入x和y所在的列。
ggplot(data = attitude) +
geom_point(mapping = aes(x = complaints, y = learning))
那么擅威,如何對(duì)散點(diǎn)進(jìn)行分類(lèi)呢壕探?我們采用CO2數(shù)據(jù)。
> 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屬性郊丛,設(shè)置為Plant列李请,這樣可以對(duì)不同的Plant對(duì)應(yīng)的散點(diǎn)應(yīng)用不同的顏色。
ggplot(data = CO2) +
geom_point(mapping = aes(x = conc, y = uptake, color = Plant))
同樣厉熟,還可以指定size參數(shù)导盅,使散點(diǎn)大小與某一參數(shù)相關(guān)。
ggplot(data = CO2) +
geom_point(mapping = aes(x = conc, y = uptake, color = Plant, size=conc))
指定shape參數(shù)揍瑟,使散點(diǎn)的形狀與某一參數(shù)相關(guān)白翻。
ggplot(data = CO2) +
geom_point(mapping = aes(x = conc, y = uptake, color = Plant, size=conc, shape=Type))
下面使用iris數(shù)據(jù)展示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參數(shù)中绢片,還可以指定透明度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èn)的設(shè)置,如果我們想自定義散點(diǎn)的形狀底循、顏色等參數(shù)時(shí)邓梅,該怎么辦呢础钠?
引入color參數(shù),可以自行設(shè)置顏色;使用shape參數(shù)可以自定義形狀尚卫,各形狀對(duì)應(yīng)的序號(hào)如下四瘫。注意到color等參數(shù)是與mapping參數(shù)并列的西饵。
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"))
2.3.3.2. 多圖布局
如何將不同分類(lèi)的變量繪制到不同的圖上蜈膨,實(shí)現(xiàn)多圖布局呢?
在geom_point函數(shù)后用“+”連接facet_wrap()函數(shù)等舔,其中首個(gè)參數(shù)為用于分類(lèi)的變量前加“~”倦炒,nrow參數(shù)表示每行布局的圖像數(shù)。
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)
如果需要用兩個(gè)變量實(shí)現(xiàn)多圖布局软瞎,可使用facet_grid()函數(shù)指定行列對(duì)應(yīng)的變量逢唤,用“~”分隔。
ggplot(data = CO2) +
geom_point(mapping = aes(x = conc, y = uptake, color=Plant)) +
facet_grid(Type ~ Treatment)
若“~”前后的參數(shù)換為“.”涤浇,則只在列或行進(jìn)行多圖布局鳖藕。
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)
散點(diǎn)圖常常會(huì)出現(xiàn)點(diǎn)重疊的情況,尤其是數(shù)據(jù)四舍五入后作圖只锭。通過(guò)調(diào)整參數(shù)position = "jitter"著恩,可以避免這種網(wǎng)格化,為每個(gè)點(diǎn)添加少量隨機(jī)噪聲。因?yàn)闆](méi)有兩個(gè)點(diǎn)可能會(huì)接收到相同數(shù)量的隨機(jī)噪聲喉誊,所以這就使避免了散點(diǎn)堆積的情況邀摆。
ggplot(data = CO2) +
geom_point(mapping = aes(x = conc, y = uptake, color=Plant), , position = "jitter") +
facet_grid(Type ~ Treatment)
主要參考文獻(xiàn):# R for Data Science
https://r4ds.had.co.nz/data-visualisation.html