R语言|绘制主成分分析PCA图
2021-04-27 本文已影响0人
维凡生物
主成分分析(Principal Component Analysis,PCA), 是一种统计方法。通过正交变换将一组可能存在相关性的变量转换为一组线性不相关的变量,转换后的这组变量叫主成分。
主成分分析试图在力保数据信息丢失最少的原则下,用较少的综合变量代替原本较多的变量,而且综合变量间互不相关,在群落分析中,PCA分析运用较多
小编今天给大家简单分享一下R绘制PCA分析图的方法。
otu_table.txt
image.png
Group.txt
image.png
具体代码如下:
1.加载vegan包和ggplot2包,设置颜色;
install.packages("vegan")
library (vegan) #加载vegan包
library (ggplot2)#加载ggplot包
color=c( "#3C5488B2","#00A087B2",
"#F39B7FB2","#91D1C2B2",
"#8491B4B2", "#DC0000B2",
"#7E6148B2","yellow",
"darkolivegreen1", "lightskyblue",
"darkgreen", "deeppink", "khaki2",
"firebrick", "brown1", "darkorange1",
"cyan1", "royalblue4", "darksalmon",
"darkgoldenrod1", "darkseagreen", "darkorchid")
2.读入文件,对比物种数据 Hellinger 转化前后的差异;
otu <- read.delim("otu_table.txt", sep = '\t', row.names = 1, stringsAsFactors = FALSE, check.names = FALSE)
otu<-t(otu)
#对比物种数据 Hellinger 转化前后的差异
otu_hel <- decostand(otu, method = 'hellinger')
#使用 Hellinger 转化后的数据
pca_sp2 <- rda(otu_hel, scale = FALSE)
3.提取特征值;
#特征值提取
pca_exp2 <- pca_sp2$CA$eig / sum(pca_sp2$CA$eig)
pc1 <- paste('PC1:', round(pca_exp2[1]*100, 2), '%')
pc2 <- paste('PC2:',round(pca_exp2[2]*100, 2), '%')
pca2=pca_sp2[["CA"]][["u"]][,c(1,2)]
map2<-read.table(file="group.txt",header=T,sep="\t",row.names=1)
merged2<-merge(pca2,map2,by="row.names",all.x=TRUE)
4.使用ggplot包绘制PCA图;
p <- ggplot(data = merged2, aes(PC1, PC2)) +
geom_point(aes(color = group)) +
stat_ellipse(aes(fill =group), geom = 'polygon', level = 0.95, alpha = 0.1, show.legend = FALSE) +
scale_color_manual(values =color[1:length(unique(map2$group))]) +
scale_fill_manual(values = color[1:length(unique(map2$group))]) +
theme(panel.grid.major = element_line(color = 'gray', size = 0.2), panel.background = element_rect(color = 'black', fill = 'transparent'),
plot.title = element_text(hjust = 0.5), legend.title = element_blank()) +
labs(x = pc1, y = pc2) +
geom_vline(xintercept = 0, color = 'gray', size = 0.5) +
geom_hline(yintercept = 0, color = 'gray', size = 0.5)
p
image.png