0

I have dendrogram which I cut the branches of for a given depth cutoff. I then want to plot it using ggplot and limit the flipped y-axis:

set.seed(10)
mat <- matrix(rnorm(24*10,mean=1,sd=2),nrow=24,ncol=10)
dend <- as.dendrogram(hclust(dist(mat)))

I save the depth of the dendrogram for limiting the plot:

require(data.tree)
dend.depth <- as.Node(dend)$plotHeight

I cut the dendrogram

depth.cutoff <- 11
dend <- cut(dend,h=depth.cutoff)$upper

I plot the cut dendrogram using dendextend

require(dendextend)
gg.dend <- as.ggdend(dend)

Plotting gg.dend without limiting the y-axis produces branches that do not end at the same line:

require(ggplot2)

ggplot(gg.dend,labels=F)+scale_y_reverse()+coord_flip()

enter image description here

So I thought I can fix that by limiting the y-axis to be between dend.depth and depth.cutoff. But this doesn't come out well:

ggplot(gg.dend,labels=F)+scale_y_reverse(lim=c(dend.depth,tol.level))+coord_flip()

enter image description here Any idea?

dan
  • 6,048
  • 10
  • 57
  • 125

2 Answers2

0

Something like this? Use "hang parameter" and give it a negative value to level off all the branches

set.seed(10)
mat <- matrix(rnorm(24*10,mean=1,sd=2),nrow=24,ncol=10)
dend <- as.dendrogram(hclust(dist(scale(mat), method = "euclidean"), method="ward.D2"))
depth.cutoff <- 11
dend2 <- cut(dend, h=depth.cutoff)$upper
nodePar <- list(lab.cex = 0.6, pch = c(NA, 19), cex = 0.7, col = "blue")
plot(hang.dendrogram(dend2, hang = - 0.1),horiz = T, nodePar = nodePar )
Mandar
  • 1,659
  • 1
  • 10
  • 14
0

Manipulating the gg.dend$seqments data.frame does it:

set.seed(10)
mat <- matrix(rnorm(24*10,mean=1,sd=2),nrow=24,ncol=10)
dend <- as.dendrogram(hclust(dist(mat)))
require(data.tree)
dend.depth <- as.Node(dend)$plotHeight
depth.cutoff <- 11
dend <- cut(dend,h=depth.cutoff)$upper
require(dendextend)
gg.dend <- as.ggdend(dend)
require(ggplot2)
ggplot(gg.dend,labels=F)+scale_y_reverse()+coord_flip()

enter image description here

Change vertical segments that lead to leaves to end at the highest leaf

leaf.heights <- dplyr::filter(gg.dend$nodes,!is.na(leaf))$height
leaf.seqments.idx <- which(gg.dend$segments$yend %in% leaf.heights)
gg.dend$segments$yend[leaf.seqments.idx] <- max(gg.dend$segments$yend[leaf.seqments.idx])
ggplot(gg.dend,labels=F)+scale_y_reverse()+coord_flip()

enter image description here

dan
  • 6,048
  • 10
  • 57
  • 125