Search code examples
rvegan

NMDS plot with vegan not coloured by groups


I try to use a script to plot NMDS that worked perfectly before...but I changed of R version (R 4.1.2 on Ubuntu 20.04) and I cannot get anymore NMDS coloured graphs.

I get this error "species scores not available"

I get the good NMDS representation, but I cannot get it colored according "TypeV" -see below code - (I got it before)

mydata TESTNMDS.csv

data <- read.table('TESTNMDS.csv',header = T, sep = ",")
coldit=c( "#FFFF33","#E141B9",  "#33FF66", "#3333FF")
abundance.matrix <- data[,3:50]
for (j in ncol(abundance.matrix):1) if (colSums(abundance.matrix[j]) < 1) 
bundance.matrix <-abundance.matrix[ , -j]
dist_data<-vegdist(abundance.matrix^0.5, method='bray')
nmds <- metaMDS(dist_data, trace =TRUE, try=500)
plot(nmds)
> plot(nmds)  
species scores not available

If I try to plot with various represenstation (that worked before) I cannot get color

orditorp(nmds,display="sites",col=coldit[data$TypeV])
ordipointlabel(nmds, display = "sites", cex=1, pch=16, col=coldit[datat$TypeV])
ordispider(nmds, groups=data$TypeV,label=TRUE)
ordihull(nmds, groups=data$TypeV, lty="dotted")

enter image description here

THANKS A LOT !


Solution

  • The main problem is that R no longer automatically converts character data to factors so you have to do that explicitly. Here is your code with a few simplifications:

    library(vegan)
    data <- read.table('TESTNMDS.csv',header = T, sep = ",")
    data$TypeV <- factor(data$TypeV)   # make TypeV a factor
    coldit=c( "#FFFF33","#E141B9",  "#33FF66", "#3333FF")
    abundance.matrix <- data[,3:50]
    idx <- colSums(abundance.matrix) > 0
    abundance.matrix <- abundance.matrix[ , idx]
    dist_data<-vegdist(abundance.matrix^0.5, method='bray')
    nmds <- metaMDS(dist_data, trace =TRUE, try=500)
    plot(nmds)
    orditorp(nmds,display="sites",col=coldit[data$TypeV])
    

    You do not need a loop to eliminate columns since R is vectorized.