Skip to content
Prev 87353 / 398502 Next

creating dendrogram from cluster hierarchy

Martin Maechler schrieb:
Unfortunately the hierarchy is created by another program than R. This 
is the reason why the only available data for the hclust or dendrogram 
object creation are the merge-matrix and the agglomeration heights. So 
as.dendrogram() does not work here.
I adapted the source code of hclust.R and a quick (and VERY dirty) 
solution is as follows:

hierarchy2dendrogram <- function(hierarchy) {
 tree <- list(merge = hierarchy[,1:2],
             height= hierarchy[,3],
             order = seq(1:(dim(hierarchy)[1]+1)),
         method=NULL,
         call = match.call(),
         dist.method = "whatever")
  class(tree) <- "hclust"
  return(tree)
}
my.merge <- matrix(c(-1,-2,-3,1), ncol=2, byrow=TRUE)
my.height <- c(0.5, 1)
my.hierarchy <- cbind(my.merge, my.height)
my.hclust.object <- hierarchy2dendrogram(my.hierarchy)
plot(my.hclust.object)

Perhaps there exists a "cleaner" solution which also returns the optimal 
order (if I am right the ordering is accomplished by the Fortran 
function "hcass2") but the above works fine for me.

Thanks a lot and best regards,
Timo