我有一个数据框(按物种矩阵排列),如下所示:
SP1 SP2 SP3 SP4
US 5 6 2 5
US 5 6 2 5
UK 5 6 2 5
AUS 5 6 2 5
我尝试使用ggplot2创建一个95%置信度多边形/椭圆的PCoA图(主坐标分析)。我得到了基础包的代码,但我想在ggplot2中使用它。我需要对每个国家/地区进行唯一的颜色编码以及每个具有国家和传说的相应颜色代码的椭圆。
#My current code
require(vegan)
df <- as.matrix(df[,-1]) #Use this to convert dataframe to matrix
row.names(df) <- df[,1]#Use this to convert dataframe to matrix
dfjd <- vegdist(df, method = "jaccard")
dfpca <- cmdscale(dfjd, eig = TRUE, k = 2)
explainvar1 <- round(dfpca$eig[1] / sum(dfpca$eig), 2) * 100
explainvar2 <- round(dfpca$eig[2] / sum(dfpca$eig), 2) * 100
sum.eig <- sum(explainvar1, explainvar2)
plot(dfpca$points[ ,1], dfpca$points[ ,2],
xlab = paste("PCoA 1 (", explainvar1, "%)", sep = ""),
ylab = paste("PCoA 2 (", explainvar2, "%)", sep = ""),
pch = 16, cex = 2.0, type = "n", cex.lab = 1.5, cex.axis = 1.2, axes = FALSE)
axis(side = 1, labels = T, lwd.ticks = 2, cex.axis = 1.2, las = 1)
axis(side = 2, labels = T, lwd.ticks = 2, cex.axis = 1.2, las = 1)
abline(h = 0, v = 0, lty = 3)
box(lwd = 2)
points(dfpca$points[ ,1], dfpca$points[ ,2],pch = 19, cex = 1, bg = "gray", col = "grey")
ordiellipse(dfpca, rownames(df), kind = "se",conf = .95,col = NULL)