3

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!

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
qfd
  • 778
  • 2
  • 10
  • 24
  • `hclust` is used for clustering and as you mention in your question you already have your clusters. What do you need `hclust` for? This question seems unclear. – LyzandeR Sep 17 '15 at 14:53
  • I would like to use the as.phylo function to display my clusters.but that takes a hclust object, I am not using the hclust algorithm, I have a separate algorithm which produces the clsuters and their membership. – qfd Sep 17 '15 at 14:55
  • Therefore, you simply cannot use `as.phylo` if you know it only requires an `hclust` object. The right question is 'how can i display my clusters properly?' or 'how can i produce a plot similar to as.phylo with my data?'. – LyzandeR Sep 17 '15 at 14:57
  • you are right. I will edit my question – qfd Sep 17 '15 at 14:57
  • It would also be good to show some research you ve done as many people on SO want that and probably a picture of how the as.phylo graph looks like. – LyzandeR Sep 17 '15 at 14:58
  • Assuming you can create hclust (from variables which can have a distance measure defined on them) - then it can be done using the dendextend+circlize R packages with the circlize_dendrogram function (see here: http://stats.stackexchange.com/questions/4062/how-to-plot-a-fan-polar-dendrogram-in-r ) – Tal Galili Sep 17 '15 at 20:33

1 Answers1

2

(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

Community
  • 1
  • 1
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • Thanks Tal this is really helpful but what I am trying to do is create hclust ; i have a distance matrix but applying hclust on this distance matrix will yield different results. I guess a simple example in the above example if I want to manually change for example the "Valiant" leaf to combine with the Mercs; is that possible ? – qfd Sep 17 '15 at 20:47
  • Hi @qfd, your distance matrix is what hclust uses. After that, you can turn hclust to dendrogram, and then assign new labels (after loading the dendextend package). Please use the function dput to create objects that reproduce your situation. – Tal Galili Sep 17 '15 at 20:58