-1

Suppose you cluster a Matrix that has a headerline in R, using hclust.

Usually one would get a labeled picture, so to speak, a dendrogram. Is there a way to make the labels of the vectors (which are in the headerline) appear within the dendrogramm?

bdemarest
  • 14,397
  • 3
  • 53
  • 56
newnewbie
  • 993
  • 2
  • 11
  • 26
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to help us to help you. – sgibb Sep 05 '13 at 18:27

2 Answers2

3

I suppose you mean column names with headerline. Here a small example:

set.seed(123)
## create example matrix
m <- matrix(runif(200), ncol=10)
## create column names (A-G)
colnames(m) <- LETTERS[1:10]
## calculate distance matrix (transpose matrix, because dist use rows as individual samples)
d <- dist(t(m))
## clustering distance matrix
h <- hclust(d)
## plot it
plot(h)

enter image description here

sgibb
  • 25,396
  • 3
  • 68
  • 74
  • Yes I meant about that, thank you. Ahm...is the actual Tree the only return value I get with hclust? Is there not something like a table which tells me which vector is in which cluster? – newnewbie Sep 06 '13 at 12:59
  • 1
    Have a look at `?hclust`. Maybe you are interested in the `merge`, `order` or `height` return value. – sgibb Sep 06 '13 at 14:15
  • Not really, actually. I had a look at them, but what I actually want is something Like a list of clusters, each cluster telling me which vectors belong to it. Is that even possible? – newnewbie Sep 06 '13 at 14:19
  • I actually have some trouble reading dendrograms: looking at your picture, I do not see how many clusters are in there: 2, 3 or 4? – newnewbie Sep 06 '13 at 14:20
  • 1
    The answer: It depends. The number of clusters are depend on the height where you cut the tree. Do you look for a clustering like `kmeans`? – sgibb Sep 06 '13 at 14:23
  • No I am looking for hierarchical clustering specifically. But the idea still is: you feed hclust a lot of data and get, in return, a number of clusters containing those vectors. Is not that so? – newnewbie Sep 06 '13 at 14:38
  • 1
    As I already said, it depends on the height where you cut the tree. In my example: 2 clusters on 2.2, 3 on 2.0, 10 on 1.2 – sgibb Sep 06 '13 at 14:48
0

To get, from a hierarchical clustering hc, a clustering in exactly k clusters, use cutree(hc, k) [translated: cut the clustering tree at height to get k clusters). This will exactly give you the desired vector length n, with contents from {1,...k}. This applies both to hclust() and to agnes() results, the latter from package cluster.

Martin Mächler
  • 4,619
  • 27
  • 27