5

I would like to create a dendrogram plot with horizontal labels, but having the leaves hang according to their height, instead of just drop to the edge of the plot.

Example:

par(mfrow = c(1,2))
hc <- hclust(dist(USArrests), "ave")
plot(hc) # a plot with hanging branches
plot(as.dendrogram(hc), horiz = TRUE) # a horizontal plot, but the branches are not hanging

enter image description here

Any suggestion on how this can be programmed?

Thanks.

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • I think you can do something close to this using the last example in `?dendrogram`. – Ferdinand.kraft Jun 13 '13 at 14:08
  • Hi Ferdinand, I admit I don't see how. Can you expand on your idea? – Tal Galili Jun 13 '13 at 15:05
  • 1
    Using the the `edgetext` attribute, to be set for every edge using `dendrapply`. It's just an idea, I don't have the time now to pursue this. :-( – Ferdinand.kraft Jun 13 '13 at 18:04
  • [Have you seen this](http://stackoverflow.com/questions/6673162/reproducing-lattice-dendrogram-graph-with-ggplot2)? It goes over dendograms in `lattice` and `ggplot2`. – nograpes Jun 25 '13 at 23:16

2 Answers2

4

You can change the value of hang in the as.dendrogram function.

par(mfrow = c(1,2))
hc <- hclust(dist(USArrests), "ave")
plot(hc)
plot(as.dendrogram(hc, hang=0.02), horiz = TRUE)
Enrique Ramos
  • 86
  • 1
  • 1
  • 5
1

For the record, I've implemented a hang.dendrogram function (in the dendextend package), to allow hanging a dendrogram also after is was created (and not only during the changing from hclust to a dendrogram). Here is how to use it:

install.packages("dendextend")
library(dendextend)

dend <- as.dendrogram(hclust(dist(USArrests), "ave"))
par(mar = c(5,5,5,5))
plot(hang.dendrogram(dend), horiz = TRUE)

enter image description here

Tal Galili
  • 24,605
  • 44
  • 129
  • 187