I created the dendrogram after starting the hierarchical analysis of clustering in R using the code below. Now I'm trying to color the labels according to another factor variable, which is saved as a vector. The closest I came to achieve this is the color code of the branches using the ColourDendrogram function in the sparcl package. If possible, I would prefer to color the code with labels. I found answers to similar questions at the following links Colored branches of the dendrogram using an existing column and Coloring branches in the dendrogram to R , but I could not decide how to convert the sample code for my purpose. The following are some examples of data and code.
> dput(df) structure(list(labs = c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "b1", "b2", "b3", "b4", "b5", "b6", "b7"), var = c(1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L), td = c(13.1, 14.5, 16.7, 12.9, 14.9, 15.6, 13.4, 15.3, 12.8, 14.5, 14.7, 13.1, 14.9, 15.6, 14.6), fd = c(2L, 3L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 4L, 2L, 1L, 4L, 3L, 3L)), .Names = c("labs", "var", "td", "fd" ), class = "data.frame", row.names = c(NA, -15L)) df.nw = df[,3:4] labs = df$labs d = dist(as.matrix(df.nw)) # find distance matrix hc = hclust(d, method="complete") # apply hierarchical clustering plot(hc, hang=-0.01, cex=0.6, labels=labs, xlab="") # plot the dendrogram hcd = as.dendrogram(hc) # convert hclust to dendrogram plot(hcd, cex=0.6) # plot using dendrogram object Var = df$var # factor variable for colours varCol = gsub("1","red",Var) # convert numbers to colours varCol = gsub("2","blue",varCol) # colour-code dendrogram branches by a factor library(sparcl) ColorDendrogram(hc, y=varCol, branchlength=0.9, labels=labs, xlab="", ylab="", sub="")
Any advice on how to do this would be greatly appreciated.