How to make a PCoA with 95% confidence polygons using ggplot2 in R?

荒凉一梦 提交于 2019-12-14 02:27:08

问题


I have a dataframe (site by species matrix)that looks like this:

            SP1      SP2     SP3     SP4
    US       5       6       2       5
    US       5       6       2       5
    UK       5       6       2       5
   AUS       5       6       2       5

I'm trying to create a PCoA plot (Principal Coordinate Analysis) with 95% confidence polygons/ellipses using ggplot2. I got the code for base package, but I want it in ggplot2. I need to uniquely color code each country along with each ellipse having the corresponding color code for the country and the legends.

#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) 

来源:https://stackoverflow.com/questions/42694173/how-to-make-a-pcoa-with-95-confidence-polygons-using-ggplot2-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!