talgalili / dendextend

Extending R's Dendrogram Functionality
153 stars 29 forks source link

Fix rect.dendrogram #119

Closed talgalili closed 2 months ago

talgalili commented 5 months ago

See: https://stackoverflow.com/questions/78136216/strange-behaviour-with-rect-dendrogram-in-dendextend-package

kalaivaniraju commented 4 months ago

Apart from two rectangles being drawn, the correct number of rectangles / clusters is also sometimes erroneous.

For example, using the same code below, depending on the hclust.out and h_cut, we get erroneous clusters (4, instead of the correct 3) or double rectangle clusters. For reference of correct clustering, please see the reference dotted vertical line segment drawn at the same height.

For reference, the plots were generated by setting cut_level to 0.5 and 0.7 in the same hclust.out.

hclust.out <- hclust(distance_matrix, method = clustering_method)
h_cut <- cut_level * max(hclust.out$height)

png("example_dend.png")
par(mar = c(5, 1, 1, 10), xpd = TRUE)
dend2 <- as.dendrogram(hclust.out)
dend2 %>% plot(horiz = TRUE, bty = "L")
dend2 %>% rect.dendrogram(h = h_cut, lty = 5, lwd = 0, col = rgb(0.8, 0.8, 0.8, 0.25), horiz = TRUE)
segments(x0 = h_cut, y0 = par("usr")[4], x1 = h_cut, y1 = 0, lty = 2)
dev.off()

covariates_corrected_500_euclidean_complete_0_5_dend covariates_corrected_500_euclidean_complete_0_7_dend

talgalili commented 4 months ago

Thanks @kalaivaniraju If you want to propose a PR I'd be happy to review it.

alecbuetow commented 2 months ago

Hey @kalaivaniraju! I have a fix for the two rectangle problem, but I am unable to recreate your issue. Is the distance_matrix and clustering_method that you used available anywhere?

alecbuetow commented 2 months ago

I was able to fix the issue with a placeholder distance_matrix and clustering_method, no need for the real values!

talgalili commented 2 months ago

The issue is now fixed. THANKS @alecbuetow !