How to designate terminal nodes of a cut dendrogram?

I used the following code to cut the dendrogram at a specific height. The problem I am facing is that when I cut the dendrogram, I cannot figure out how to add labels to the nodes. How can I cut tagged dendrogram using program R?

library(Heatplus) cc=as.dendrogram(hclust(as.dist(mat),method="single")) cutplot.dendrogram(cc,h=20) 
+4
source share
3 answers

After a considerable search in the help documentation for ?dendrogram I came across a dendrapply function that contains an example to make something very similar. Here is your solution based on modifying the example in ?dendrapply :

Create a dendrogram and cut the height h=20 :

 dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave")) chc <- cut(dhc, h=20)$upper 

Define a vector with new labels and the newLab function, which modifies the individual label node. Then pass this to dendrapply :

 newLabels <- paste("Custom", 1:22, sep="_") local({ newLab <<- function(n) { if(is.leaf(n)) { a <- attributes(n) i <<- i+1 attr(n, "label") <- newLabels[i] } n } i <- 0 }) nhc <- dendrapply(chc, newLab) labels(nhc) [1] "Custom_1" "Custom_2" "Custom_3" "Custom_4" "Custom_5" "Custom_6" [7] "Custom_7" "Custom_8" "Custom_9" "Custom_10" "Custom_11" "Custom_12" [13] "Custom_13" "Custom_14" "Custom_15" "Custom_16" "Custom_17" "Custom_18" [19] "Custom_19" "Custom_20" "Custom_21" "Custom_22" plot(nhc) 

enter image description here

+6
source

Here is a modified solution for what Andri wrote, but using a new package called " dendextend " built specifically for this kind of thing.

In the presentations and vignettes of the package, you can see many examples in the "Use" section at the following URL: https://github.com/talgalili/dendextend

Here is a solution on this:

 # define dendrogram object to play with: dhc <- as.dendrogram(hc <- hclust(dist(USArrests), "ave")) chc <- cut(dhc, h=20)$upper # loading the package require(dendextend)# let add some color: # change labels with a simple assignment: labels(chc) <- paste("Custom", 1:22, sep="_") plot(chc) 

To install the package (since I have not downloaded it in CRAN yet), use:

 #################### ## installing dendextend for the first time: if (!require('installr')) install.packages('installr'); require('installr') ## install.Rtools() # run this if you are using Windows and don't have Rtools require2(devtools) install_github('dendextend', 'talgalili') require2(Rcpp) install_github('dendextendRcpp', 'talgalili') 

Best, Tal

0
source
 cc$labels 

This is the vector of all elements in the dendrogram.

 cc$labels <- myVector 

You can add to your own vector to change labels.

-1
source

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


All Articles