1

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) 
Share
  • 395
  • 7
  • 19
  • That example is not close to being reproducible. What is `dat.pcoa`, what is `datjd`? Is `dfjd` the matrix you should above?? – Axeman Mar 09 '17 at 12:45
  • I have edited the code above. It should work now – Share Mar 09 '17 at 13:34
  • What is `datjd`? Please run your code in a clean session... – Axeman Mar 09 '17 at 13:35
  • Apologies. Corrections have been made – Share Mar 09 '17 at 13:47
  • `Error in vegdist(df, method = "jaccards") : invalid distance method`. I'm out, good luck. – Axeman Mar 09 '17 at 13:51
  • I ran this again a fresh screen and I'm able to plot it. I have made comments to help users know where I need help and if I'm running into trouble. @Axeman: I understand your chagrin. I appreciate the time you took to get back to me on the errors. Thankyou – Share Mar 09 '17 at 14:08
  • It looks like your code is working now. So you want a similar one in ggplot2? – Rspacer Mar 09 '17 at 16:47
  • Yes, I need one in ggplo2 with the colors and ellipse – Share Mar 09 '17 at 16:48
  • This should be all you need. http://stackoverflow.com/questions/42799838/follow-up-plotting-ordiellipse-function-from-vegan-package-onto-nmds-plot-creat/43566309#43566309 – J.Con Apr 23 '17 at 01:47

0 Answers0