Overlaying clustering results on ordination

I need to superimpose the clusters created by cutting the dendrogram of a given level of similarity into the result of ordination (NMDS). I looked through ade4 and vegan without finding any explicit solutions to this problem.

I am currently using Primer-e (see screenshot below), but I find that the graphics are a bit limited. Any point in the right direction is welcome.

enter image description here

+4
source share
1 answer

This is pretty easy with vegan , and I have a post that explains some of these details, but not a bit about clustering.

Here is a brief example, I assume that you can translate it to all the packages / code you use.

Download package and dataset

require(vegan) data(dune) 

Calculate the difference matrix and copy it, cutting the dendrogram to give 3 groups

 dij <- vegdist(dune) ## bray curtis dissimilarity clu <- hclust(dij, method = "average") grp <- cutree(clu, 3) 

Look grp

 R> grp 2 13 4 16 6 1 8 5 17 15 10 11 9 18 3 20 14 19 12 7 1 1 1 2 1 1 1 1 3 2 1 1 1 1 1 2 2 3 1 1 

and note that now this gives the cluster membership (second row) for each sample (top row) in the dataset.

Next install NMDS

 set.seed(2) ## setting a seed to make this reproducible ord <- metaMDS(dune) 

In this example, I will color the dots according to cluster membership, so I need to define a color vector, one per cluster

 col <- c("red2", "green4", "mediumblue") 

Now I can use grp and col to create a vector of color names for each point (selection) I am indexing on col with grp . For instance:.

 R> col[grp] [1] "red2" "red2" "red2" "green4" "red2" [6] "red2" "red2" "red2" "mediumblue" "green4" [11] "red2" "red2" "red2" "red2" "red2" [16] "green4" "green4" "mediumblue" "red2" "red2" 

All remains are to draw an NMDS order and add points and legend. I suppress any construction in the plot() call, so I can have more control over adding points on the next line. The third line just adds the legend.

 plot(ord, type = "n", display = "sites") points(ord, col = col[grp], bg = col[grp], pch = 21) legend("topright", legend = paste("Cluster", 1:3), col = col, pt.bg = col, bty = "n", pch = 21) 

The resulting figure should look like this:

NMDS plot with cluster membership overlain


Refresh . To add convex hulls for each cluster of points to the ordination diagram, you can use the ordihull() function. Continuing the example above, we add convex hulls as follows

 ordihull(ord, groups = grp, display = "sites") 

At this point, the drawing will look like the one shown below

adding convex hulls for the clusters


Note : vegan methods of a higher level plot() are designed specifically for quick and dirty display of ordination and, as such, do not accept color vectors or graphic symbols. instead, we expect you to create lower-level flow methods for your graphs, such as points() , which I use here.

+4
source

Source: https://habr.com/ru/post/1434353/


All Articles