绘制一个带统计信息花里胡哨的PCOA图,喜欢的小伙伴可以用以下代码尝试
rm(list=ls())
pacman::p_load(tidyverse,ggrepel,vegan,ape,ggsignif,patchwork,multcomp)
#-----------------------------------------------------------------
data <- read.csv("data.xls", header = T,check.names = F,
sep="\t",row.names = 1) %>% t()
data[is.na(data)] <- 0
pcoa <- vegdist(data,method = "bray") %>%
pcoa(correction = "none", rn = NULL)
groups <- read.table("group2.txt",sep = "\t",header = T) %>%
as.list()
PC1 = pcoa$vectors[,1]
PC2 = pcoa$vectors[,2]
pcoadata <- data.frame(rownames(pcoa$vectors),
PC1,PC2,groups$Group)
colnames(pcoadata) <-c("sample","PC1","PC2","group")
#-----------------------------------------------------------------------------
pcoadata$group <- factor(pcoadata$group,levels = c("Apoe","Ob","Wt"))
yf <- pcoadata
yd1 <- yf %>% group_by(group) %>% summarise(Max = max(PC1))
yd2 <- yf %>% group_by(group) %>% summarise(Max = max(PC2))
yd1$Max <- yd1$Max + max(yd1$Max)*0.1
yd2$Max <- yd2$Max + max(yd2$Max)*0.1
res1 <- aov(PC1~group,data = pcoadata) %>%
glht(linfct=mcp(group="Tukey")) %>% cld(alpah=0.05)
res2 <- aov(PC2~group,data = pcoadata) %>%
glht(linfct=mcp(group="Tukey")) %>% cld(alpah=0.05)
test <- data.frame(PC1 = res1$mcletters$Letters,PC2 = res2$mcletters$Letters,
yd1 = yd1$Max,yd2 = yd2$Max,group = yd1$group)
test$group <- factor(test$group,levels = c("Apoe","Ob","Wt"))
#-------------------------------------------------------------------------
p1 <- ggplot(pcoadata, aes(PC1, PC2)) +
geom_point(aes(colour=group,fill=group),size=4)+
labs(x=(floor(pcoa$values$Relative_eig[1]*100)) %>%
paste0("PC1 ( ", ., "%", " )"),
y=(floor(pcoa$values$Relative_eig[2]*100)) %>%
paste0("PC2 ( ", ., "%", " )")) +
theme(text=element_text(size=12))+
geom_vline(aes(xintercept = 0),linetype="dotted")+
geom_hline(aes(yintercept = 0),linetype="dotted")+
theme(panel.background = element_rect(fill='white', colour='black'),
axis.title.x=element_text(colour='black', size=12),
axis.title.y=element_text(colour='black', size=12),
axis.text=element_text(colour='black',size=12),
legend.title=element_blank(),
legend.key.height=unit(0.6,"cm"),
legend.position = c(0.75, 0.95),legend.direction = "horizontal")
p1
#-----------------------------------------------------------------------------------
p2 <- ggplot(pcoadata,aes(group,PC1)) +
geom_boxplot(aes(fill = group))+
geom_jitter(shape=16,size=1.5,position=position_jitter(0.2))+
geom_text(data = test,aes(x = group,y = yd1,label = PC1),
size = 5,color = "black",fontface = "plain")+
theme(panel.background = element_rect(fill='white',
colour='black'))+
theme(axis.ticks.length = unit(0.4,"lines"),
axis.ticks = element_line(color='black'),
axis.line = element_line(colour = "black"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_text(colour='black',size=10,face = "plain"),
axis.text.x=element_blank(),
legend.position = "none")+coord_flip()
p2
#--------------------------------------------------------------------------------------
p3 <- ggplot(pcoadata,aes(group,PC2)) +
geom_boxplot(aes(fill = group)) +
geom_jitter(shape=16,size=1.5,position=position_jitter(0.2))+
geom_text(data = test,aes(x = group,y = yd2,label = PC2),
size = 5,color = "black",fontface = "plain")+
theme(panel.background = element_rect(fill='white',
colour='black'))+
theme(axis.ticks.length = unit(0.4,"lines"),
axis.ticks = element_line(color='black'),
axis.line = element_line(colour = "black"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.x=element_text(colour='black',size=10,angle = 0,
vjust = 1,hjust = 0.5,face = "plain"),
axis.text.y=element_blank(),
legend.position = "none")
p3
otu.adonis=adonis(data~group,data = pcoadata,distance = "bray")
p4 <- ggplot(pcoadata,
aes(PC1, PC2))+
geom_text(aes(x = -0.5,
y = 0.6,
label = paste("PERMANOVA:\ndf = ",
otu.adonis$aov.tab$Df[1],"\nR2 = ",
round(otu.adonis$aov.tab$R2[1],4),
"\np-value = ",
otu.adonis$aov.tab$`Pr(>F)`[1],
sep = "")),size = 4) +theme_bw() +
xlab(NULL) + ylab(NULL) +
theme(panel.grid=element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())
p2+p4+p1+p3 +
plot_layout(heights = c(1,4),widths = c(4,1),ncol = 2,nrow = 2)