I add my data so that it can be reproducible, it's also synthesized. It is only important to mention that I have an error, I did not know how to apply the function row.names = 1 in the metadata file, when the data is reproduced in this way, I'm sorry and thank you very much in advance.
Family <- tibble::tribble(
~SampleID, ~K00370, ~K00371, ~K00374, ~K02567, ~K02568, ~K00368, ~K15864, ~K04561, ~K02305, ~K00376, ~K02585, ~K02593, ~K02596, ~K02597, ~K04488, ~K15790, ~K15861, ~K02588, ~K02586, ~K02591, ~K00531, ~K10944, ~K10945, ~K10946, ~K10535, ~K00362, ~K00363, ~K03385, ~K00367, ~K10534, ~K00372, ~K00360, ~K00366, ~K17877, ~K01120,
"1A", 12.60826933, 12.59387294, 12.58242827, 8.658177059, 8.654818552, 12.39259191, 8.875738182, 13.30169117, 13.23015428, 12.40313895, 8.986755702, 8.7956609, 8.7956609, 9.649657193, 12.80857018, 8.637907728, 9.908449129, 8.915573375, 8.892945265, 8.912764195, 3.723758308, 3.915575591, 4.002697437, 4.373425552, 1.932860333, 13.03212083, 12.97064336, 5.793988069, 5.692819382, 0L, 11.21187223, 6.080985352, 6.139827357, 0, 6.196363174,
"1C", 10.85111666, 10.51628714, 10.47316521, 12.59133496, 12.59100967, 10.81480582, 6.640661761, 11.34807149, 10.67816214, 10.77641355, 9.010564151, 8.823865852, 8.901467628, 9.815606527, 11.28993854, 8.819429773, 9.883052886, 8.975107452, 8.96710843, 8.971113485, 0, 0, 0, 0, 0, 13.08434153, 13.07040844, 7.840243987, 7.965980112, 0L, 11.03644689, 0, 3.600508638, 0, 7.101635393,
"1D", 14.86428566, 14.85699297, 14.84651048, 8.732362469, 8.732362469, 11.77211189, 9.49327795, 12.05851906, 9.958018276, 10.80103387, 7.890709098, 0, 7.8576836, 0, 14.9651165, 0, 11.8931705, 7.890709098, 7.890709098, 7.890709098, 0, 0, 0, 0, 0, 15.31434467, 15.10448672, 7.954575106, 9.369882273, 0L, 12.99900903, 9.853011196, 7.189987452, 0, 9.655793912,
"1E", 11.0375149, 11.02563759, 10.85857454, 9.449081993, 9.365885069, 10.4609361, 8.994861357, 11.59181604, 10.57867658, 10.86525433, 9.336468717, 8.433274173, 8.640040373, 9.346919584, 12.89570338, 8.475161923, 8.281734733, 9.52620972, 9.352116764, 9.416376674, 4.058928093, 5.116206199, 5.148427408, 5.938243369, 3.265569708, 13.06931013, 11.97984879, 9.619675763, 9.284860642, 0L, 12.1506354, 7.929788004, 8.413523389, 1.359918286, 7.101224149,
"1F", 10.11131442, 10.13226188, 9.59041767, 9.312694087, 9.263115646, 10.82558104, 9.910083683, 11.87462456, 11.79567509, 11.20638875, 9.924723347, 9.301216597, 9.351282699, 10.25850935, 12.04002777, 9.125348724, 7.517278994, 9.888851117, 9.828271014, 9.845008917, 6.038077222, 6.82590961, 6.22870436, 7.679112526, 5.575229252, 11.17826569, 11.18830242, 8.629959046, 6.98840685, 0L, 11.07243943, 9.572438501, 8.242273249, 1.217887296, 6.65420049,
"1G", 9.674460458, 9.786108569, 9.424338792, 9.459911487, 9.439267486, 12.06450569, 9.836597751, 13.16834862, 13.14486284, 12.20565036, 10.02056228, 9.51450029, 9.55344182, 10.46910632, 11.52636421, 9.265885181, 7.556454349, 9.876631962, 9.81844183, 9.847830258, 4.18206088, 5.897309418, 5.501740779, 6.617577237, 4.393054339, 10.73415185, 10.65195313, 8.307809442, 4.393054339, 0L, 10.53312916, 8.268226781, 7.197634024, 1.280563118, 5.141764139,
"1H", 11.13164554, 11.13753508, 11.11412997, 8.358429761, 8.344239451, 11.7697941, 9.037626526, 12.7256916, 12.66836574, 11.82740417, 9.264479094, 8.511405023, 8.529434909, 9.392145317, 12.02810675, 6.605407729, 5.448841874, 9.051418179, 9.017327612, 9.030047864, 1.503665225, 3.640694665, 3.530423688, 4.130980108, 2.482604369, 11.46338527, 11.43281794, 9.512149513, 8.187288626, 0L, 9.81303876, 1.503665225, 8.556064165, 0, 7.538684939,
"2A", 9.936729753, 9.966825855, 9.678799275, 9.666943625, 9.646804153, 11.76982895, 9.896682653, 12.89677964, 12.84537157, 12.04014767, 10.21692639, 9.800123586, 9.825089342, 10.75721758, 11.77712663, 9.656429448, 7.82089312, 10.02742472, 10.00650343, 10.0038668, 3.822442496, 5.525944895, 5.207639494, 6.229847407, 4.026238555, 11.2124103, 11.23571212, 8.323228785, 6.339071682, 0L, 11.07256764, 9.015404785, 7.458923089, 0.342907156, 7.521356687,
"2B", 10.03797367, 10.48972259, 9.908996955, 10.35381509, 10.34327074, 11.74553691, 9.607964823, 12.96965286, 12.9996672, 12.35458675, 10.39612571, 10.07446524, 10.09537891, 11.05425242, 11.7826571, 9.984103388, 5.872607476, 10.42554338, 10.37827263, 10.3854404, 5.378641163, 6.027246822, 5.852057903, 6.650157365, 4.326170316, 11.16095852, 10.75929562, 9.481557201, 5.83121139, 0L, 10.70768899, 6.432136121, 7.657833318, 0.500112485, 4.803797853,
"2C", 10.1021309, 10.11534801, 9.919125231, 9.392338518, 9.370456601, 12.92440641, 9.619586736, 14.00078982, 13.97347911, 13.03827193, 9.867984033, 9.423371076, 9.454905899, 10.33964174, 11.73698176, 9.214061506, 7.741414612, 9.670220649, 9.642084891, 9.661236713, 3.148574103, 5.735104701, 5.750246569, 6.221490572, 3.678261287, 11.03231667, 11.01674834, 8.28437339, 6.839467423, 0L, 10.82760851, 8.602771221, 7.89236426, 0.643380395, 7.538328575,
"2D", 11.05615199, 11.07379325, 11.00635667, 9.007346856, 8.964430518, 10.56420436, 9.114538677, 11.64500702, 11.3385747, 11.26183102, 9.305824713, 8.747095943, 8.79687069, 9.679905969, 12.46847754, 8.49429229, 7.052864854, 9.237267286, 9.139121597, 9.196856236, 4.586244598, 6.314516801, 6.35705229, 6.869137642, 4.267408634, 11.75645523, 11.65888686, 7.994514985, 6.859307282, 0L, 10.81365271, 7.976439244, 7.651392497, 0, 6.907802637,
"2E", 11.13555413, 11.13267537, 11.09302763, 10.63177977, 10.62242889, 11.93663405, 9.197744277, 12.78255337, 12.52296656, 11.73013096, 7.584362739, 5.920828681, 6.805243723, 6.502936043, 12.38686885, 6.146354359, 9.230425941, 7.326674674, 7.231475903, 7.303457403, 1.546963788, 0, 0, 0, 0.714286675, 12.83914921, 12.616306, 7.743782277, 9.445863761, 0L, 11.83403931, 7.255870968, 6.666550302, 0, 9.260877781,
"2F", 10.5798746, 10.58467245, 10.51474914, 8.903874863, 8.849099905, 12.39306664, 9.682786329, 13.33395894, 13.25018442, 12.46898421, 8.911532812, 8.573074632, 8.648333823, 9.554157178, 12.65582839, 6.367690307, 4.860095264, 9.018234003, 8.930501712, 8.989578633, 2.27022436, 4.419172626, 4.419172626, 4.92206419, 2.608488015, 11.80943425, 11.70424366, 9.984532993, 8.911532812, 0L, 11.07102804, 4.860095264, 8.110230974, 0, 8.523962638,
"2H", 10.13751737, 10.13408134, 9.804426603, 7.69264147, 7.548657741, 12.29649025, 8.918164107, 13.29192095, 13.20868481, 12.25807231, 9.07691191, 7.906448711, 8.111380944, 8.756247661, 12.31519584, 7.534826767, 7.047646656, 9.328118647, 9.136509849, 9.22960214, 4.650531707, 6.998105354, 6.809945662, 7.714278939, 5.332710084, 11.96522201, 10.85078306, 9.577010383, 7.076574887, 0L, 11.405516, 7.823550376, 9.283324739, 1.478384964, 6.715012564
)
Metadata<- data.frame (tibble::tribble(
~SampleID, ~SamplingPoint, ~Depth, ~Month,
"1A", "CEA1", "80 cm", "July",
"1C", "CEA4", "Interstitial", "July",
"1D", "CEA1", "80 cm", "August",
"1E", "CEA3", "80 cm", "August",
"1F", "CEA5", "80 cm", "August",
"1G", "CEA2", "80 cm", "September",
"1H", "CEA4", "80 cm", "September",
"2A", "CEA1", "80 cm", "July",
"2B", "CEA2", "Interstitial", "July",
"2C", "CEA4", "Interstitial", "July",
"2D", "CEA1", "80 cm", "August",
"2E", "CEA3", "80 cm", "August",
"2F", "CEA5", "80 cm", "August",
"2H", "CEA4", "80 cm", "September")
)
attach(Family)
Family <- Family[,-1]
rownames(Family) <- SampleID
#> Warning: Setting row names on a tibble is deprecated.
Metadata$Month <- factor(Metadata$Month,
levels = c("July", "August", "September"))
#remove the rare microbes. It keeps only the microbes that are present in at least 10% of the samples
dim(Family)
#> [1] 14 35
Family <- Family[,colMeans(Family) >=.1]
dim(Family)
#> [1] 14 34
library(vegan)
#> Loading required package: permute
#> Loading required package: lattice
#> This is vegan 2.5-6
#NMDS
#autotransform=F evita la transformaci?n de datos
#sratmax=0.999999 para que el an?lisis no se detenga antes de tiempo cuando se tienen muchos datos
Family.mds <- metaMDS(Family, k=2, distance="bray", trymax=100, zerodist="add", autotransform=F)
#> Run 0 stress 9.969131e-05
#> Run 1 stress 9.654804e-05
#> ... New best solution
#> ... Procrustes: rmse 0.04793172 max resid 0.1307909
#> Run 2 stress 9.928457e-05
#> ... Procrustes: rmse 0.07320599 max resid 0.219269
#> Run 3 stress 9.843455e-05
#> ... Procrustes: rmse 0.07156589 max resid 0.2137064
#> Run 4 stress 8.916943e-05
#> ... New best solution
#> ... Procrustes: rmse 0.01976362 max resid 0.05322951
#> Run 5 stress 9.765706e-05
#> ... Procrustes: rmse 0.0552266 max resid 0.1693111
#> Run 6 stress 9.116397e-05
#> ... Procrustes: rmse 0.05286723 max resid 0.1613987
#> Run 7 stress 9.727769e-05
#> ... Procrustes: rmse 0.05528396 max resid 0.169486
#> Run 8 stress 9.562458e-05
#> ... Procrustes: rmse 0.01910763 max resid 0.0546893
#> Run 9 stress 9.496489e-05
#> ... Procrustes: rmse 0.05522583 max resid 0.1693072
#> Run 10 stress 9.169977e-05
#> ... Procrustes: rmse 0.08777776 max resid 0.201423
#> Run 11 stress 9.440087e-05
#> ... Procrustes: rmse 0.08776635 max resid 0.2013933
#> Run 12 stress 9.266926e-05
#> ... Procrustes: rmse 0.03070879 max resid 0.08994358
#> Run 13 stress 9.785632e-05
#> ... Procrustes: rmse 0.02474976 max resid 0.0644543
#> Run 14 stress 9.767342e-05
#> ... Procrustes: rmse 0.05434868 max resid 0.1663609
#> Run 15 stress 9.866632e-05
#> ... Procrustes: rmse 0.009986105 max resid 0.02805647
#> Run 16 stress 8.944125e-05
#> ... Procrustes: rmse 0.005120099 max resid 0.01392708
#> Run 17 stress 9.614291e-05
#> ... Procrustes: rmse 0.04602012 max resid 0.1387744
#> Run 18 stress 9.988593e-05
#> ... Procrustes: rmse 0.05475556 max resid 0.1677288
#> Run 19 stress 9.727364e-05
#> ... Procrustes: rmse 0.04896145 max resid 0.1484392
#> Run 20 stress 9.447012e-05
#> ... Procrustes: rmse 0.01861441 max resid 0.05321458
#> Run 21 stress 9.553666e-05
#> ... Procrustes: rmse 0.0877595 max resid 0.2013674
#> Run 22 stress 9.61222e-05
#> ... Procrustes: rmse 0.08782066 max resid 0.2015415
#> Run 23 stress 9.733919e-05
#> ... Procrustes: rmse 0.05430509 max resid 0.1662195
#> Run 24 stress 9.191404e-05
#> ... Procrustes: rmse 0.05521235 max resid 0.1692595
#> Run 25 stress 9.95102e-05
#> ... Procrustes: rmse 0.05518633 max resid 0.1691812
#> Run 26 stress 9.679203e-05
#> ... Procrustes: rmse 0.05521129 max resid 0.1692602
#> Run 27 stress 9.950208e-05
#> ... Procrustes: rmse 0.05074253 max resid 0.1543305
#> Run 28 stress 8.953321e-05
#> ... Procrustes: rmse 0.05185096 max resid 0.1580135
#> Run 29 stress 9.721993e-05
#> ... Procrustes: rmse 0.01022478 max resid 0.02749834
#> Run 30 stress 9.369102e-05
#> ... Procrustes: rmse 0.05492537 max resid 0.168296
#> Run 31 stress 8.603495e-05
#> ... New best solution
#> ... Procrustes: rmse 0.08775786 max resid 0.2013639
#> Run 32 stress 9.385069e-05
#> ... Procrustes: rmse 0.1367656 max resid 0.3853064
#> Run 33 stress 9.949237e-05
#> ... Procrustes: rmse 0.1363297 max resid 0.3837648
#> Run 34 stress 9.65593e-05
#> ... Procrustes: rmse 0.0001028226 max resid 0.0002124013
#> ... Similar to previous best
#> *** Solution reached
#> Warning in metaMDS(Family, k = 2, distance = "bray", trymax = 100, zerodist =
#> "add", : stress is (nearly) zero: you may have insufficient data
stressplot(Family.mds)

#add the metadata information
##THIS HAS TO BE IN THE SAME ORDER IN THE METADATA FILE AND IN THE COUNTS FILE
colvec<- c("yellowgreen","turquoise4", "tomato2")
plot(Family.mds, type="n", xlim=c(-.5,.5), ylim=c(-0.6,0.6))
pl <-ordiellipse(Family.mds, Metadata$Month, kind="se", conf=0.95, lwd=2, col="gray30", label=T)

#plot with ggplot
data.scores <- as.data.frame(scores(Family.mds))
data.scores$site <- rownames(data.scores)
data.scores$grp <- Metadata$Month
head(data.scores)
#> NMDS1 NMDS2 site grp
#> 1 -0.03379469 -0.003350833 1 July
#> 2 0.14117940 -0.044950983 2 July
#> 3 0.08974835 0.127384866 3 August
#> 4 -0.03380353 -0.003408347 4 August
#> 5 -0.03384985 -0.003424872 5 August
#> 6 -0.03385658 -0.003408284 6 September
library(ggplot2)
NMDS <- data.frame(MDS1 = Family.mds$points[,1], MDS2 = Family.mds$points[,2],group=Metadata$Month)
#NMDS.mean <- aggregate(NMDS[,1:2],list(group=group),mean)
veganCovEllipse<-function (cov, center = c(0, 0), scale = 1, npoints = 100)
{
theta <- (0:npoints) * 2 * pi/npoints
Circle <- cbind(cos(theta), sin(theta))
t(center + scale * t(Circle %*% chol(cov)))
}
#attach(NMDS.mean)
df_ell <- data.frame()
for(g in levels(NMDS$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,],
veganCovEllipse(pl[[g]]$cov,pl[[g]]$center,pl[[g]]$scale)))
,group=g))
}
ggplot() +
geom_path(data=df_ell, aes(x=NMDS1, y=NMDS2,colour=group), size=1, linetype=1)+
geom_point(data=data.scores,aes(x=NMDS1,y=NMDS2,colour=grp),size=1.5) + # add the point markers
theme_light()+
ylab("NMDS2")+
xlab("NMDS1")+
#geom_text(data=data.scores,aes(x=NMDS1,y=NMDS2,label=site),size=6,vjust=0) +
scale_colour_manual(values=c("July" = "yellowgreen", "August" = "turquoise4", "September" = "pink"))

Created on 2021-06-02 by the reprex package (v0.3.0)