I have a dendrogram
:
set.seed(10)
mat <- matrix(rnorm(20*10),nrow=20,ncol=10)
dend <- as.dendrogram(hclust(dist(mat)))
And given a depth cutoff:
I'd like to cut all branches that are to the right to that cutoff.
depth.cutoff <- 4.75
I'd like to cut all branches to the right of the dashed line:
plot(dend,horiz = TRUE)
abline(v=depth.cutoff,col="red",lty=2)
And to end up with this dendrogram
:
The closest I got was using ape
's drop.tip
, but the problem with that is if my depth.cutoff
includes all leaves, as in this example, it returns NULL
.
Perhaps anyone knows if and how I can delete elements from the nested list
which represents my dendrogram
if their depth
is below depth.cutoff
?
Alternatively, perhaps I can convert the dendrogram
to a data.frame
, which also lists the depth
of each node
(including leaves which will have depth
=0), remove all rows with depth
<
depth.cutoff
from that data.frame
, and then convert that back to a dendrogram
?