Load required packages and data.

suppressPackageStartupMessages(library(vegan))
suppressPackageStartupMessages(library(ade4))
suppressPackageStartupMessages(library(vegan3d))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(ggordiplots))
suppressPackageStartupMessages(library(rgl))
load("otu.rda")
load("sam.rda")

Check that samples in otu table and sample data table are in the same order.

all(rownames(otu)==rownames(sam))
## [1] TRUE

Calculate Bray distance matrix and PCoA ordination.

dist.bray <- vegdist(otu, method = "bray")
callieze <- !is.euclid(dist.bray)
ord <- cmdscale(dist.bray, k = 3, add = callieze)

Plot the ordination. Use the gg_ordiplot function from the ggordiplots package to include spiders and standard error ellipses depicting 95% confidence intervals.

plt <- gg_ordiplot(ord, groups = sam$Type, kind = "se", conf = 0.95, spiders = TRUE, plot = FALSE)
plt$plot + ggtitle("PCoA based on bray distances")

Make the same plot with vegan3d functions.

open3d() # Opens a graphic device for the plot.
## wgl 
##   8
par3d(windowRect = c(20, 40, 1000, 1200)) # left, top, right, bottom in pixels
my.color <- c("red", "blue")
ordirgl(ord, size=4, col = my.color[as.numeric(sam$Type)]) # plot points
with(sam, orglspider(ord, Type, col = my.color[1:2], scaling = "sites")) # add spiders
with(sam, orglellipse(ord, Type, col = my.color[1:2], kind = "se", conf = 0.95, scaling = "sites")) # add ellipses
rglwidget() # You cannot run this line in RStudio.