本期內容為:跟著iMeta學作圖 | 三元圖
網址:https://github.com/iMetaScience/iMetaPlot/tree/main/230127ternary
三元圖(Ternary plot)是描述三個變量之和為常數的質心圖,其核心原理是:
- 等邊三角形內任意一點到三角形三邊的距離之和等于其中一邊上的高(常數)
- 過等邊三角形內任意一點分別向三條邊作平行線,按順時針方向或逆時針方向讀取平行線在各邊所截取之三條線段,三條線段之和等于該等邊三角形任一邊之長(常數)
案例1. 不同區域的分布差異
圖片描述:
- 三元圖展示不同區域(Bulk soil, Rhizosphere soil, Endosphere)的樣本的群落結構在門水平上的差異情況,每個圓點代表一個OTU,OTU的大小、顏色和位置分別代表其相對豐度、菌門水平名稱和分組情況。
文中的圖片解讀:
- Bacterial community structures were clearly different in the different compartments at the phylum level.These differences were mainly driven by strong relative enrichment of Firmicutes in the endosphere-derived sequence data sets, compared to their very low abundances in the bulk and rhizosphere soils. The relative abundances of Proteobacteria and Bacteroidetes increased progressively from bulk to rhizosphere soil to the endosphere, with a concomitant decrease in those of candidate division AD3, Gemmatimonadetes and Chloroflexi, which collectively constituted <4% of endosphere communities.
- 譯文:細菌群落結構在不同的區系中明顯不同。這些差異主要是由于在內圈衍生的序列數據中,與它們在大塊土壤和根莖層土壤中的豐度非常低相比,堅定的相對富集。Proteobacteria和Bacteroidetes的相對豐度從大塊土壤到根莖層土壤再到內圈逐漸增加,同時候選部門AD3、Gemmatimonadetes和Chloroflexi的相對豐度也隨之減少,它們共同構成了內圈群落的4%以下。
案例2. 不同區域的分布差異
圖片描述:
最上方的“Wild-type”和“Mutants”指明材料類型
三個頂點分別為三個區域,并在括號中指出顯著富集的OUT數量
點的大小代表三組樣品的平均相對豐度
通過顏色指示顯著富集情況:Soil = 土色, rhizosphere = 橘黃色,Root = 綠色,灰色 = 兩兩比較中未全部顯著富集的(均未富集,只相對其中一個分組富集)。
解讀:
- Ternary plots depicting compartment RA of all OTUs (>5 ‰) for WT samples (A; WT; n = 73) and mutant samples (B; nfr5-2, nfr5-3, nin-2, and lhk1-1; n = 118) across three soil batches (CAS8–CAS10). Each point corresponds to an OTU. Its position represents its RA with respect to each compartment, and its size represents the average across all three compartments. Colored circles represent OTUs enriched in one compartment compared with the others (green in root, orange in rhizosphere, and brown in root samples). Aggregated RAs of each group of enriched OTUs (root-, rhizosphere- and soil-enriched OTUs) in each compartment for the WT samples (C; WT; n = 73) and mutant samples (D; nfr5-2, nfr5-3, nin-2, lhk1-1; n = 118) are shown. In each compartment, the difference from 100% RA is explained by OTUs that are not significantly enriched in a specific compartment.
- 譯文:三元圖描述了WT樣品(A;WT;n=73)和突變體樣品(B;nfr5-2、nfr5-3、nin-2和lhk1-1;n=118)在三個土壤批次(CAS8-CAS10)中所有OTU(>5‰)的區間RA。每個點對應于一個OTU。其位置代表其相對于每個區室的RA,其大小代表所有三個區室的平均值。彩色圓圈代表與其他區間相比在一個區間富集的OTU(根部為綠色,根莖層為橙色,根部樣本為棕色)。WT樣品(C;WT;n = 73)和突變體樣品(D;nfr5-2,nfr5-3,nin-2,lhk1-1;n = 118)的每組富集的OTU(根、根瘤和土壤富集的OTU)的聚合RAs被顯示。在每個區室中,與100%RA的差異由在特定區室中沒有明顯富集的OTU解釋。
加載R包
options(repos = list(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if (!require("devtools"))
install.packages('devtools')
if (!require("ggtern"))
install.packages('ggtern')
install.packages("devtools")
devtools::install_github("microbiota/amplicon")
# 加載包
library(amplicon)
library(ggtern)
讀取數據
dim(tax_phylum) # 查看數據維度,共3個樣品各6個重復共18列數據
rela_tax <- apply(tax_phylum, 2, function(x) x/sum(x)) # 絕對豐度/對每一列的豐度總和得到每個物種的相對豐度
head(rela_tax)
> head(rela_tax)
KO1 KO2 KO3 KO4 KO5 KO6 OE1
Proteobacteria 0.617653929 0.46110070 0.577771665 0.594885187 0.712199416 0.64016542 0.50491371
Actinobacteria 0.252858323 0.39708672 0.275890968 0.276946549 0.170047613 0.23005945 0.26495472
(Unassigned) 0.067362257 0.07211575 0.039484396 0.051790005 0.038210699 0.07031817 0.07538712
Firmicutes 0.016590704 0.02050448 0.015293956 0.032593709 0.007632137 0.01740450 0.04969151
Bacteroidetes 0.028983760 0.02590566 0.075470174 0.030094192 0.051914536 0.02360610 0.03459409
Acidobacteria 0.003707922 0.00348076 0.002489016 0.002129589 0.001860521 0.00356092 0.01019826
絕對豐度/對每一列的豐度總和得到每個物種的相對豐度
rela_tax <- apply(tax_phylum, 2, function(x) x/sum(x))
> head(rela_tax)
KO1 KO2 KO3 KO4 KO5 KO6 OE1
Proteobacteria 0.617653929 0.46110070 0.577771665 0.594885187 0.712199416 0.64016542 0.50491371
Actinobacteria 0.252858323 0.39708672 0.275890968 0.276946549 0.170047613 0.23005945 0.26495472
(Unassigned) 0.067362257 0.07211575 0.039484396 0.051790005 0.038210699 0.07031817 0.07538712
Firmicutes 0.016590704 0.02050448 0.015293956 0.032593709 0.007632137 0.01740450 0.04969151
Bacteroidetes 0.028983760 0.02590566 0.075470174 0.030094192 0.051914536 0.02360610 0.03459409
Acidobacteria 0.003707922 0.00348076 0.002489016 0.002129589 0.001860521 0.00356092 0.01019826
rela_group_tax <- t(apply(rela_tax, 1,
function(x) c(mean(x[1:6]),
mean(x[7:12]), mean(x[13:18])))) # 對每個樣品的6個重復計算相對豐度均值
colnames(rela_group_tax) <- c('KO', "OE", "WT") # 對列重命名
> head(rela_group_tax)
KO OE WT
Proteobacteria 0.600629387 0.492962782 0.517005328
Actinobacteria 0.267148271 0.286652619 0.310002750
(Unassigned) 0.056546879 0.066246303 0.056116461
Firmicutes 0.018336580 0.033110576 0.008959386
Bacteroidetes 0.039329070 0.055680293 0.049950747
Acidobacteria 0.002871455 0.007273774 0.005003141
保留相對豐度較高的10個門,合并其余門的相對豐度
df <- as.data.frame(rela_group_tax)
# 對每個門額外計算一列總相對豐度用于選出高豐度的門
df$total <- rowSums(df)
# 得到排序后的數據框
sort_df <- df[order(df$total, decreasing = T),]
計算其他低豐度門的豐度總和
others <- colSums(sort_df[11:nrow(df),])
# 合并表格
df2 <- rbind(sort_df[1:10, ], others)
計算豐度并添加分組,分別代表三元圖中點的大小和顏色
df2$Abundance <- rowMeans(df2)
gp <- c(rownames(df2)[1:10],"Others")
df2$Phylum <- factor(gp, level=gp)
> head(df2)
KO OE WT total Abundance
Proteobacteria 0.600629387 0.49296278 0.517005328 1.61059750 0.80529875
Actinobacteria 0.267148271 0.28665262 0.310002750 0.86380364 0.43190182
(Unassigned) 0.056546879 0.06624630 0.056116461 0.17890964 0.08945482
Bacteroidetes 0.039329070 0.05568029 0.049950747 0.14496011 0.07248006
Cyanobacteria/Chloroplast 0.003750115 0.03581664 0.031287083 0.07085384 0.03542692
Firmicutes 0.018336580 0.03311058 0.008959386 0.06040654 0.03020327
Phylum
Proteobacteria Proteobacteria
Actinobacteria Actinobacteria
(Unassigned) (Unassigned)
Bacteroidetes Bacteroidetes
Cyanobacteria/Chloroplast Cyanobacteria/Chloroplast
Firmicutes Firmicutes
繪圖
ggtern(data=df2, aes(KO, OE, WT)) +
geom_point(aes(color=Phylum, size=Abundance)) +
theme_bw() +
theme_arrowdefault()
ggtern(df2, aes(KO, OE, WT))+
geom_point(aes(color=Phylum, size=Abundance), alpha = 0.8, show.legend = F)+
geom_mask()+ # 可將超出邊界的點正常顯示出來
theme_bw() +
theme(axis.text = element_blank(),
axis.ticks = element_blank())+
theme_arrowdefault() ## 顯示方向
參考:
http://www.lxweimin.com/p/c87f3a3334fd
https://mp.weixin.qq.com/s/B0dA8qIHkJfAstd3qGBOPA
https://github.com/iMetaScience/iMetaPlot/blob/main/230127ternary/
往期文章(總匯)--點擊鏈接進入(列出部分,并非全部)
01-[R語言可視化-精美圖形繪制系列]--精美火山圖
02-R語言可視化-精美圖形繪制系列--柱狀圖
03-R語言可視化-精美圖形繪制系列--功能富集分析
04-R語言可視化-精美圖形繪制系列—多組GO富集可視化
05-[R語言可視化-精美圖形繪制系列--堆積圖]
06-[R語言可視化-精美圖形繪制系列--組間相關性分析]
07-[R語言可視化-精美圖形繪制系列]--Mental分析
08-[R語言可視化-精美圖形繪制系列--復雜熱圖+兩圖漸變連線]-【轉載】
09-[R語言可視化-精美圖形繪制系列--桑基圖(Sankey)]
10-[R語言可視化-精美圖形繪制系列--柱狀圖誤差線標記]11-跟著NC學作圖 | 柱狀圖與相關性圖12-[R語言可視化-精美圖形繪制系列--GO、KEGG富集通路關聯圖]
13-[跟著“基迪奧生物學”作圖]--截斷圖14-[R語言可視化-精美圖形繪制系列]--顯著性箱線圖
14-2[R語言可視化]--箱線圖不同的畫法及參數設置 | 學習筆記15-[R語言可視化-精美圖形繪制系列]--組內相關性分析
16-[R語言可視化-精美圖形繪制系列]--主成分分析(PCA)
17-[跟著NC學作圖]--箱線圖(一個函數獲得Mean、SD、P值)
18-[跟著NC學作圖]--生存分析(Survival analysis)
19-[跟著NC學作圖]--散點圖20-[R語言可視化-精美圖形繪制系列]--散點圖+箱線圖組合圖
21-[跟著NC學作圖]-柱狀堆積圖22-[跟著NC學作圖]-繪制頻率分布圖(圖中圖)
22-[R語言可視化-精美圖形繪制系列]--FPI箱線圖
23-跟著NC做基因組數據分析24-使用OmicCircos包--繪制基因圈圖25-跟著iMeta學作圖 | 棒棒圖和顯著相關性散點圖
小杜的生信筆記 ,主要發表或收錄生物信息學的教程,以及基于R的分析和可視化(包括數據分析,圖形繪制等);分享感興趣的文獻和學習資料!!