Part 1 :散點圖+變量邊緣分布圖形
原文點我,感謝支持
許多文章的散點圖中,在散點圖的周圍還會有額外的單變量邊緣分布統(tǒng)計圖形(如下圖)。前面幾期我們介紹過,散點圖主要反映的是兩個變量之間的關(guān)系,而額外的邊緣分布圖則能直觀反映每個變量的分布情況。本期就來介紹實現(xiàn)這類圖形的常見方法。
示例:

Abundance of antibiotic resistance genes, intI1 gene and crAssphage in human fecal metagenomes.
[1]示例圖中散點圖的做法參見一起學畫圖:散點圖(1)— 基礎(chǔ)散點圖 — R-ggplot2復現(xiàn)Nature文章散點圖
Part 2 :圖像與代碼
方法一:ggExtra::ggMarginal() :在ggplot2散點圖基礎(chǔ)上快速添加
ggExtra包可以通過常規(guī)的install.packages("ggExtra")語句安裝,相關(guān)參數(shù)見下:
ggMarginal (p, data, x, y,
type = c("density", "histogram", "boxplot", "violin", "densigram"),
margins = c("both", "x", "y"), size = 5,
..., xparams = list(), yparams = list(),
groupColour = FALSE,
groupFill = FALSE)
| 參數(shù) | 作用 |
|---|---|
| p | ggplot2繪制的圖像對象 |
| data | 如果沒有傳入p的話,則可以使用此參數(shù)傳入數(shù)據(jù) |
| x | 如果沒有傳入p的話,指定在data中用作x軸的數(shù)據(jù) |
| y | 如果沒有傳入p的話,指定在data中用作y軸的數(shù)據(jù) |
| type | 邊緣分布圖形類型:密度圖density、直方圖histogram、箱型圖boxplot、小提琴圖violin、密度直方圖densigram |
| margins | 指定添加哪個變量的分布圖形:both/x/y,默認both |
| size | 散點圖 : 邊緣圖的比例。size=1即表示邊緣分布圖和主散點圖一樣大 |
| fill | 指定邊緣分布圖形的填充顏色 |
| color | 指定邊緣分布圖形的邊界線條顏色 |
| xparams | 指定x軸的分布圖型的顏色 |
| yparams | 指定y軸的分布圖型的顏色 |
| groupFill | 分布圖的填充顏色按組別指定 |
| groupColour | 分布圖的邊界線條顏色按組別指定 |
基礎(chǔ)使用示例
#導入包
library(ggplot2)
library(ggExtra)
#常規(guī)使用ggplot2作圖
p <- ggplot(`iris`, aes_string('Sepal.Length', 'Sepal.Width')) +
aes_string(colour = 'Species') +
geom_point() +
theme_bw()+
theme(legend.position = "bottom")
#使用ggMarginal添加邊緣分布圖形,ggplot2繪制的圖p作為變量傳入(如果沒有使用ggplot2繪制圖形,則此處也可以自行傳入數(shù)據(jù)繪制)
p1 <- ggMarginal(p+ggtitle("type='histogram'"), type="histogram")
p1

參數(shù)表中提出到的5種不同的統(tǒng)計圖形示例如下:

在此基礎(chǔ)上,我們可以通過調(diào)整相關(guān)參數(shù)優(yōu)化圖形。幸運的是,該包的作者團隊同時也開發(fā)了支持實時制作并可導出R代碼的網(wǎng)頁版工具:https://daattali.com/shiny/ggExtra-ggMarginal-demo/

這樣一來我們可以預先在網(wǎng)頁上調(diào)整好所需要的樣式,然后利用生成的R代碼在本地生成所需格式和尺寸的圖像
使用上圖網(wǎng)頁工具自動生成的代碼在本地運行得到的結(jié)果如下:

方法二:ggPubr::ggscatterhist( )
ggPubr是一個基于ggplot2開發(fā)的面向出版級繪圖的R包,ggscatterhist()中的參數(shù) margin.* 可以為散點圖進一步配置邊緣分布統(tǒng)計圖形,其語法與ggplot2類似,目前支持:密度圖density,直方圖histogram,箱型圖boxplot三種邊緣分布圖形相關(guān)參數(shù)及作用見官方文檔:http://rpkgs.datanovia.com/ggpubr/reference/ggscatterhist.html
# 函數(shù)聲明
ggscatterhist(data, x, y, group = NULL, color = "black", fill = NA,
palette = NULL, shape = 19, size = 2, linetype = "solid",
bins = 30, margin.plot = c("density", "histogram", "boxplot"),
margin.params = list(), margin.ggtheme = theme_void(), margin.space = FALSE,
main.plot.size = 2, margin.plot.size = 1, title = NULL,
xlab = NULL, ylab = NULL, legend = "top", ggtheme = theme_pubr(),
print = TRUE, ...)
基礎(chǔ)示例:

#導入包
library(ggpubr)
#作圖
gp1<-ggscatterhist(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", size = 3, alpha = 0.6)
gp1
在此基礎(chǔ)上,可以對參數(shù)進行調(diào)整來繪制更為符合需求的圖片

#導入包
library(ggpubr)
#作圖
gpDensity <- ggscatterhist(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.params = list(fill = "Species", color = "black", size = 0.3)
)
gp1p
# 此圖并沒有指定margin.plot = "density",只是在上圖gp1的基礎(chǔ)行進行了填充
# 實際上,如果指定margin.plot = "density" 得到的圖形是一樣的

#導入包
library(ggpubr)
#作圖
gp4 <- ggscatterhist(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.plot = "histogram", #指定類型為histogram
margin.params = list(fill = "Species", color = "black", size = 0.3))
gp4
方法三:分別作圖最后利用grid.arrange()進行拼接
文中Part 1的示例圖像應該就是用此方法組合而成的,此方法不作更多介紹,感興趣的朋友可以參考使用grid.arrange()排列圖形的方法:https://cran.rproject.org/web/packages/egg/vignettes/Ecosystem.html
Part 3 :地毯圖
以上我們介紹了在散點圖周圍添加單變量邊緣分布統(tǒng)計圖的方法,邊緣分布圖的目的是直觀反映單變量的分布情況,而地毯圖也可以達到類似的目的 相關(guān)參數(shù)及作用以及更高級的用法可參考官方文檔:https://ggplot2.tidyverse.org/reference/geom_rug.html
geom_rug(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
outside = FALSE,
sides = "bl",
length = unit(0.03, "npc"),
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
示例圖像:

#導入包
library(ggplot2)
#常規(guī)使用ggplot2作圖
prug <- ggplot(`iris`, aes_string('Sepal.Length', 'Sepal.Width')) +
aes_string(colour = 'Species') +
geom_point() +
theme_bw()+
theme(legend.position = "bottom")+
geom_rug(alpha = 0.5, position = "jitter")
prug
參考:
[1] Karkman A, P?rn?nen K, Larsson DGJ. Fecal pollution can explain antibiotic resistance gene abundances in anthropogenically impacted environments. Nat Commun. 2019 Jan 8;10(1):80. doi: 10.1038/s41467-018-07992-3. PMID: 30622259; PMCID: PMC6325112.
http://rpkgs.datanovia.com/ggpubr/reference/ggscatterhist.html
https://cran.rproject.org/web/packages/egg/vignettes/Ecosystem.html