rplotmds

labeling nmds plot by column values in r


I ran metaMDS and want to plot and color code by a grouping based on certain data frame characters. In my original data frame, df$yr are years and df$2 are sites. I want to color by the years.

caltmds <- metaMDS(df[,3:12], k=3)
plot(caltmds, type = 'n')
cols <- c("red2", "mediumblue")
points(caltmds, col = cols[df$yr])

I also tried from this post:

scl <- 3
colvec <- c("red2", "mediumblue")
plot(caltmds, type = "n", scaling = scl) 
with(df, points(caltmds, display = "sites", col = colvec[yr], pch = 21, bg = colvec[yr]))
text(caltmds, display = "species", cex = 0.8, col = "darkcyan")
with(df, legend("topright", legend = levels(yr), bty = "n", col = colvec, pch = 21, pt.bg = colvec))

Nothing plots


Solution

  • #DATA
    df1 = mtcars
    
    mycolors = df1$cyl   #Identify the grouping vector
    
    library(vegan)
    m = metaMDS(df1)
    
    x = scores(m)  #Extract co-ordinates
    
    plot(x, col = as.numeric(as.factor(mycolors)))