1

when I use default plot() for hclust object, it places leaves' labels at different distances, just as I need:

data(mtcars)
plot(hclust(dist(mtcars)))

plot.hclust

But when I do the same for dendrogram object, it aligns all labels to the same level:

plot(as.dendrogram(hclust(dist(mtcars))))

plot.dendrogram

How to disable this alignment and make it behave exactly like for hclust? I tried hang=0 but it makes all "leaves" to have zero length:

plot.dendrogram with hang=0

Vasily A
  • 8,256
  • 10
  • 42
  • 76

2 Answers2

1

You can do this by adjusting the hang parameter.

plot(as.dendrogram(hclust(dist(mtcars)), hang = 0))
MKR
  • 1,620
  • 7
  • 20
  • thanks for the suggestion! the problem with `hang=0` is that it also sets the length of all "leaves" to zero (I'll add the picture to the question). Do you know how to solve that issue? – Vasily A Apr 14 '20 at 07:57
  • I see the problem. Have you tried to set `hang=0.1` or any other small number? – MKR Apr 14 '20 at 08:24
  • yes I tried: unfortunately setting `hang` to anything above 0 makes all labels aligned – Vasily A Apr 14 '20 at 08:35
  • oh actually not exactly: with really small number like `hang=0.01` I do have non-aligned labels, but then again the length of the "leaves" is close to 0 – Vasily A Apr 14 '20 at 08:38
  • `plot(hclust(dist(mtcars)))` seems to just add branch of fixed length to the leave. Therefore, the leaves at the end go below 0 (which doesn't make sense). The version with `as.dendrogram` ends at 0. Thus, you might need to live with short leaves at the end. It may be worth looking into the `ggtree` package for more flexibility in plotting trees. – MKR Apr 14 '20 at 08:46
  • Seems you're right. I wanted to avoid additional packages but apparently I'll have to. Will try `ggtree`. – Vasily A Apr 14 '20 at 08:57
-1
plot(hclust(dist(mtcars)), hang = -1)

From the help page:

hang The fraction of the plot height by which labels should hang below the rest of the plot. A negative value will cause the labels to hang down from 0.

Yanlingz
  • 1
  • 1
  • 1
    well your answer takes `hclust` object while I need it to work with the `dendrogram` object. And the output is exactly what I *don't* need: all labels are aligned to the same level, while I need to keep different distances. – Vasily A Aug 31 '20 at 04:24