library(tidyverse)
theme_set(theme_test())
library(ape)
library(ade4)
pcoa.data <- iris %>% mutate(Sepal.Length = scale(Sepal.Length),
Sepal.Width = scale(Sepal.Width),
Petal.Length = scale(Petal.Length),
Petal.Width = scale(Petal.Width))
tab.dist <- dist(pcoa.data[,-5])
pcoa <- pcoa(tab.dist)
pcoa_eig <- pcoa$values
sample_site <- pcoa$vectors%>%as.data.frame()
names(sample_site)[1:2] <- c("x","y")
sample_site <- data.frame(sample_site,iris$Species)
sample_site%>%ggplot()+
geom_point(aes(x,y,color=iris.Species),size = 1.5)+
geom_vline(xintercept = 0, color = 'gray', size = 0.4) +
geom_hline(yintercept = 0, color = 'gray', size = 0.4) +
stat_ellipse(aes(x,y,color = iris.Species,fill = iris.Species),
geom ="polygon",level = 0.95,size = 0.5,alpha = 0.2)+
labs(x = paste('PCoA1: ', round(100 * pcoa_eig[1,2], 2), '%'),
y = paste('PCoA2: ', round(100 * pcoa_eig[2,2], 2), '%'))
ggplot2作图之PcoA
于 2022-09-24 22:11:48 首次发布