3

I am using the hclust function:

points <- data.frame(ID = c('A','B','C','D','E'), 
                 x = c(3,4,2.1,4,7), 
                 y = c(6.1,2,5,6,3))
d <- dist(as.matrix(points[, 2:3])) 
clusters <- hclust(d,method = "complete")
plot(clusters, labels=points$ID)

Is there a way to show the values where the points are joined (or the node values (where the dissimilarity between the samples is minimal))?

I want my plot to look like the one on the picture.

Note: The values showed on the dendrogram are not the correct ones.

Dendogram

2 Answers 2

5

My R package TBEST has a function that can add two color annotations to a hclust object. For your convenience, I am pasting codes below, so you can use them independent of any packages.

hc2axes<-function (x) {
    A <- x$merge
    n <- nrow(A) + 1
    x.axis <- c()
    y.axis <- x$height
    x.tmp <- rep(0, 2)
    zz <- match(1:length(x$order), x$order)
    for (i in 1:(n - 1)) {
       ai <- A[i, 1]
       if (ai < 0) 
           x.tmp[1] <- zz[-ai]
       else x.tmp[1] <- x.axis[ai]
           ai <- A[i, 2]
       if (ai < 0) 
           x.tmp[2] <- zz[-ai]
       else x.tmp[2] <- x.axis[ai]
           x.axis[i] <- mean(x.tmp)
    }
    return(data.frame(x.axis = x.axis, y.axis = y.axis))
}



plot_height<-function (hc, height, col = c(2, 3), print.num = TRUE, float = 0.01, cex = NULL, font = NULL) 
{
    axes <- hc2axes(hc)
    usr <- par()$usr
    wid <- usr[4] - usr[3]
    bp <- as.character(round(height,2))
    rn <- as.character(1:length(height))
    bp[length(bp)] <- "height"
    rn[length(rn)] <- "edge #"
    a <- text(x = axes[, 1], y = axes[, 2] + float * wid, bp, 
        col = col[1], pos = 2, offset = 0.3, cex = cex, font = font)
    if (print.num) {
        a <- text(x = axes[, 1], y = axes[, 2], rn, col = col[2], 
            pos = 4, offset = 0.3, cex = cex, font = font)
    }
}

Once you paste these two functions, add one line to plot your dendrogram, plot(clusters,labels=points$ID);
cluster_height(clusters,height=clusters$height,print.num=F)

You can also plot the branch numbers by setting print.num=T enter image description here

Sign up to request clarification or add additional context in comments.

Comments

4

Here's one method using the dendextend package.

First, convert to hanging dendrogram

library(dendextend)
dend <- as.dendrogram(clusters) %>% hang.dendrogram()
dend <- dend %>% set_labels(points$ID[dend %>% labels()])

Now we find the x,y values for all the internal nodes

xy <- dend %>% get_nodes_xy()
is_internal_node <- is.na(dend %>% get_nodes_attr("leaf"))
is_internal_node[which.max(xy[,2])] <- FALSE
xy <- xy[is_internal_node,]

And now we plot the dendrogram and draw the labels at a slight offset

plot(dend)
text(xy[,1]+.2, xy[,2]+.2, labels=format(xy[,2], digits=2), col="red")

This gives the following plot

enter image description here

2 Comments

Thank you, just what I needed! This is the first time I hear about this library and it seems awesome.
If you mean the labels we drew with text(), then set the cex= parameter. If you mean the leaf labels, check out the dendextend introduction, it's covered there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.