1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| rm(list = ls())
library(dplyr) library(ggplot2) library(vegan)
data = read.table('data/otu.txt',header = T, row.names = 1) %>% t() %>% as.data.frame() %>% mutate(group = rep(c('AAS','ANS','NAS','NNS'),each = 3))
pca = prcomp(data[,1:ncol(data)-1],scale. = TRUE)
pca.var = pca$sdev^2
pca.var.per = round(pca.var/sum(pca.var)*100,2)
data.frame(PC = as.character(paste('PC',1:nrow(data),sep = '')), value = pca.var.per) %>% ggplot(aes(PC,value))+ geom_bar(stat = 'identity', fill = 'white', color = 'black')+ geom_hline(yintercept = pca.var.per[1]*1.1, color = 'white')+ scale_x_discrete(limits = c(paste('PC',1:nrow(data),sep = '')))+ theme_classic()+ scale_y_continuous(expand = c(0,0))+ geom_text(aes(y = value + 1,label = paste(value,'%',sep = '')),size = 2.5)+ labs(x = '',y = '',title = 'ScreePlot')+ theme(axis.text = element_text(size = 11,color = 'black'), axis.ticks = element_line(color = 'black'), plot.title = element_text(hjust = 0.5))
as.data.frame(pca$x) %>% mutate(group = data$group) %>% ggplot(aes(PC1,PC2,color = group))+ geom_point(size = 2)+ theme_classic()+ labs(x = paste('PC1(',pca.var.per[1],'%)',sep = ''), y = paste('PC2(',pca.var.per[2],'%)',sep = ''))+ stat_ellipse(level = 0.68)+ theme(axis.text = element_text(size = 11,color = 'black'), axis.ticks = element_line(color = 'black'), plot.title = element_text(hjust = 0.5))
otu = data[,1:ncol(data)-1]
dist = vegdist(otu, method = 'bray')
site = data.frame(sample = rownames(data), group = data$group)
adonis_result_dis = adonis(dist~group, site, permutations = 999) adonis_result_dis
group_name = unique(site$group)
result = data.frame()
for (i in 1:(length(group_name) - 1)) { for (j in (i + 1):length(group_name)) { group_ij = subset(site, group %in% c(as.character(group_name[i]), as.character(group_name[j]))) otu_ij = otu[group_ij$sample, ] adonis_result_otu_ij = adonis(otu_ij~group, group_ij, permutations = 999, distance = 'bray') res.temp = as.data.frame(adonis_result_otu_ij$aov.tab)[1,] rownames(res.temp) = paste(as.character(group_name[i]),'/',as.character(group_name[j])) result = rbind(result,res.temp) } }
head(result,nrow(result))
|