简体   繁体   中英

Plot horizontal dendrogram with hanging leaves? (R)

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

在此输入图像描述

Any suggestion on how this can be programmed?

Thanks.

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)

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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM