2

I have a distance matrix for ~20 elements, which I am using to do hierarchical clustering in R. Is there a way to label elements with a plot or a picture instead of just numbers, characters, etc?

So, instead of the leaf nodes having numbers, it'd have small plots or pictures.

Here is why I'm interested in this functionality. I have 2-D scatterplots like these (color indicates density)

http://www.pnas.org/content/108/51/20455/F2.large.jpg (Note that this is not my own data)

I have to analyze hundreds of such 2-D scatter plots, and am trying out various distance metrics which I'm feeding on to hclust. The idea is to quickly (albeit roughly) cluster the 2-D plots to figure out the larger patterns, so we can minimize the number of time-consuming, follow-up experiments. Hence, it'll be ideal to label the dendrogram leaves with the appropriate 2-D plots.

Asker
  • 41
  • 1
  • 4
  • what have you tried? can you add a reproducible example. this can help you http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – agstudy Dec 13 '12 at 00:57
  • @flodel if you read the question, I want to label leaves with plots. Instead of using strings or colors, I want to have small pictures, since I am clustering 2-D scatterplots by pattern. – Asker Dec 13 '12 at 01:26
  • @agstudy thanks, unfortunately, I haven't seen this in R before, that's why I'm asking. Basically, I have data that looks like this (this is not my data, btw): http://www.pnas.org/content/108/51/20455/F2.large.jpg colors indicate density ...except I have hundreds of these, and I'm trying to cluster these so I can quickly assess the different classes of behaviors....and it would be extremely helpful to have a dendrogram labeled with the appropriate plots – Asker Dec 13 '12 at 01:28
  • maybye this can help you as a first try http://gastonsanchez.wordpress.com/2012/10/03/7-ways-to-plot-dendrograms-in-r/ – agstudy Dec 13 '12 at 01:41

1 Answers1

4

There is one option :

  1. Convert your hclust using as.dendrogram
  2. use dendrapply to apply a function through the tree. The function customize the leaf.

here one example , where I color my cluster and I change the chape of the node.

hc = hclust(dist(mtcars[1:10,]))
hcd <- as.dendrogram(hc)
mycols <- grDevices::rainbow(attr(hcd,"members"))
i <- 0 
colLab <- function(n) {
    if(is.leaf(n)) {
      i <<- i + 1
      a <- attributes(n)
      attr(n, "nodePar") <-
        c(a$nodePar, list(lab.col = mycols[i],lab.bg='grey50',pch=sample(19:25,1)))
      attr(n, "frame.plot") <- TRUE
    }
    n
  }
clusDendro = dendrapply(hcd, colLab)
# make plot
plot(clusDendro, main = "Customized Dendrogram", type = "triangle")

enter image description here

Idea:

If you try to customize the node label to an map it to an url link. So when you click on the leaf name , you navigate to its image. I think it is not hard to do.

agstudy
  • 119,832
  • 17
  • 199
  • 261