Search code examples
rdendrogramhclustdendextend

display clusters in radial format


I have a list of clusters lets say from cluster 1 to cluster 3; along with their membership for example below. I would like to display the clusters in radial format. I was thinking of using the as.phylo function in the ape package to display this, but that requires creating a hclust object.If anyone knows how to do this thats much appreciated creating a hclust object or otherwise.

Many Thanks!

cl var numberOfCluster
 1  a    1
 1  b    1
 1  c    1
 1  d    1
 1  a    2
 1  b    2
 2  c    2
 2  d    2
 3  a    3
 1  b    3
 2  c    3
 2  d    3

Thanks very much!


Solution

  • (This is a copy of my answer to a similar question from "crossvalidated")

    Assuming you can create hclust (from variables which can have a distance measure defined on them) - then it can be done by combining two new packages: circlize and dendextend.

    The plot can be made using the circlize_dendrogram function (allowing for a much more refined control over the "fan" layout of the plot.phylo function).

    # install.packages("dendextend")
    # install.packages("circlize")
    library(dendextend)
    library(circlize)
    
    # create a dendrogram
    hc <- hclust(dist(datasets::mtcars))
    dend <- as.dendrogram(hc)
    
    # modify the dendrogram to have some colors in the branches and labels
    dend <- dend %>% 
       color_branches(k=4) %>% 
       color_labels
    
    # plot the radial plot
    par(mar = rep(0,4))
    # circlize_dendrogram(dend, dend_track_height = 0.8) 
    circlize_dendrogram(dend, labels_track_height = NA, dend_track_height = .4) 
    

    enter image description here