Tree cut and Rectangles around clusters for a horizontal dendrogram in R

后端 未结 3 1015
梦毁少年i
梦毁少年i 2020-12-29 14:25

I am trying to plot the results of a hierarchical clustering in R as a dendrogram, with rectangles identifying clusters.

The following code does the tr

3条回答
  •  孤独总比滥情好
    2020-12-29 14:49

    Both jlhoward and Backlin answers are good.

    What you could also try is using the dendextend package, designed exactly for this sort of thing. It has a rect.dendrogram function which works like rect.hclust, but with a horiz parameter (plus some more control over the location of the edge of the rect). For finding the relevant height you can use the heights_per_k.dendrogram function (which is much faster when also using the dendextendRcpp package)

    Here is a simple example for how you would get the same result as in the above examples (with an added bonus of colored branches, just for fun):

    install.packages("dendextend")
    install.packages("dendextendRcpp")
    
    library("dendextend")
    library("dendextendRcpp")
    
    # using piping to get the dend
    dend <- iris[,-5] %>% dist %>% hclust %>% as.dendrogram
    
    # plot + color the dend's branches before, based on 3 clusters:
    dend %>% color_branches(k=3) %>% plot(horiz=TRUE, main = "The dendextend package \n Gives extended functionality to R's dendrogram object")
    
    # add horiz rect
    dend %>% rect.dendrogram(k=3,horiz=TRUE)
    
    # add horiz (well, vertical) line:
    abline(v = heights_per_k.dendrogram(dend)["3"] + .6, lwd = 2, lty = 2, col = "blue")
    

    enter image description here

提交回复
热议问题