R: PCoA加上散点标记

菌群相对丰度数据,用bray-curtis计算样本间的距离,接着用PCOA降维并作散点图。ggrepel包可为ggplot提供geom_text_repel函数,给样本点添加标记信息。

pca pcoa的区别
PCA is a special case of PCoA with Euclidean distances.

library(vegan)
library(ggplot2)
library(ape)
library("ggrepel")

options(scipen = 3)
# "morisita", "chao", "cao"
algo = c("manhattan", "euclidean", "canberra", "bray", "kulczynski", "jaccard", "gower", "altGower", "horn", "mountford", "raup", "binomial", "mahalanobis")
for(title in algo)
{
    veg = vegdist(df, method = title)
    pcoa = pcoa(veg)

    pcoa_point = data.frame(pcoa$vectors[, c(1, 2)], 
                            Group = group)
    ## 关于分组需要自己定义
    
    xylab = paste(c("PCoA1: ", "PCoA2: "), round(pcoa$values$Relative_eig[1:2]*100, 2), "%", sep = "")

    p = 
    ggplot(pcoa_point, aes(x=Axis.1, y=Axis.2)) +
    geom_point(aes(color = Group), size=3) + 
    geom_text_repel(aes(label = rownames(pcoa_point)), size = 6) +
    theme_classic() +
    labs(x=xylab[1], y=xylab[2], color = "", title = title) +
    theme(legend.text=element_text(size=15)) +
    theme(legend.title=element_text(face='bold', size=20)) +
    theme(axis.title = element_text(size = 20)) +
    theme(axis.text = element_text(size = 20),
          axis.line = element_line(size = 1),
          axis.ticks = element_line(size = 1)) +
    theme(plot.title = element_text(face="bold", size=20))

  ggsave(p, file=paste("./Result/beta/pcoa", title, "pdf", sep="."), width = 9)
  ggsave(p, file=paste("./Result/beta/pcoa", title, "png", sep="."), width = 9)
}

一个for轮子跑完所有距离算法

更多
PCA、PCoA、NMDS、RDA和CCA比较

你可能感兴趣的:(R: PCoA加上散点标记)