Plot factors (spp) of metaMDS in ggplot

Asked

Viewed 41 times

3

I want to plot the species in the clusters of the metaMDS result using ggplot. I tried so, but gives error:Error: Aesthetics must be either length 1 or the same as the data (20): label.

library(dune)
data(dune)
sol <- metaMDS(dune)
spp<-names(dune)
mds1<-sol$points[,1]
mds2<-sol$points[,2]
sol1<-cbind(dune,mds1,mds2)
ggplot(sol1,aes(mds1,mds2))+
  geom_point()+
  geom_text(aes(label=spp))

I know why the error occurs. The number of rows and columns is different, but I don’t know how to fix it or if it can be fixed.

1 answer

3


The object sol already has all the information needed to create the multidimensional scaling Plot.

library(vegan)
data(dune)
sol <- metaMDS(dune)

In particular, see the information present within sol$species:

sol$species
#>                 MDS1        MDS2
#> Achimill -0.82281011  0.04326590
#> Agrostol  0.71096673 -0.28923350
#> Airaprae -0.52824471  1.67985459
#> Alopgeni  0.39097510 -0.58595238
#> Anthodor -0.72024342  0.65912703
#> Bellpere -0.47837443 -0.24447599
#> Bromhord -0.61896319 -0.33477103
#> Chenalbu  0.59187735 -0.92196207
#> Cirsarve -0.15182360 -0.82170787
#> Comapalu  1.28932890  0.60841273
#> Eleopalu  1.24505152  0.16150523
#> Elymrepe -0.42013221 -0.68024533
#> Empenigr -0.08839391  1.69631104
#> Hyporadi -0.41574036  1.44599800
#> Juncarti  0.91146527 -0.08307932
#> Juncbufo  0.26477479 -0.60759446
#> Lolipere -0.51198132 -0.24808035
#> Planlanc -0.70645461  0.32062556
#> Poaprat  -0.38843320 -0.25092040
#> Poatriv  -0.15905975 -0.47836891
#> Ranuflam  1.14364071  0.09955908
#> Rumeacet -0.52478430 -0.10531078
#> Sagiproc  0.14315709 -0.18744430
#> Salirepe  0.57483100  0.91107541
#> Scorautu -0.13957081  0.25000786
#> Trifprat -0.77153459  0.08563492
#> Trifrepe -0.07526533  0.04517137
#> Vicilath -0.46793723  0.54915464
#> Bracruta  0.15072189  0.18980509
#> Callcusp  1.42117957  0.38378896
#> attr(,"shrinkage")
#>      MDS1      MDS2 
#> 0.5160467 0.3713241 
#> attr(,"centre")
#>        MDS1        MDS2 
#> -0.02324753 -0.05510302

Note that all information about the coordinates of the points and their respective species is in this object. Now just create an appropriate data frame and from it create the desired chart.

sol1 <- data.frame(sol$species, spp = rownames(sol$species))

library(ggplot2)
ggplot(sol1, aes(x = MDS1, y = MDS2))+
  geom_point()+
  geom_text(aes(label = spp))

  • In my case I still need to relate the species to the places where they were collected and show in the graph. Maybe it is better to edit the question or ask a new question with a df similar to mine.

  • Exactly. The closer the data set provided is to reality and the more complete the question, the more likely someone is to answer it usefully. I find it more productive to ask a new question, because an issue like this would greatly alter the original question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.