-1

I am having a little problem in generating a cluster dendrogram.

The cluster technique is working, the question is just the graph. I would like to make minor adjustments to it.

The distance between the names on the X axis and the end of the lines with the divisions of the group is huge and covering a large part of the graph unnecessarily, I would very much like to decrease that (green markings on the image). As a result, I would like the scale on the Y axis to increase (blue markings on the image).

Would anyone know how to solve this? I searched the documentation for rect.hclust and found no arguments to make these adjustments.

To facilitate understanding, I attach the script and image of the generated dendrogram and what I would like to happen.

Thank you so much for your attention (and help)!

Image

pts <- read_excel("C:/pts.xlsx")
row.names(pts) <- c("Painting","Dance","Photo", "Cinema","Book","Music")
matrix = dist(pts, "euclidean")
group = hclust(matrix, "ward.D")
hcd <- as.dendrogram(group)
dend_data <- dendro_data(hcd, type = "rectangle")
plot(group, hang=-1)
rect.hclust(group, k=3, border="red")
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • plot(group, hang=-1) ; rect.hclust(group, k=3, border="red") is correct, you just need to resize your plotting device, or save it to png – StupidWolf Mar 07 '20 at 15:55

1 Answers1

0

I think hang=-1 works, unless I misunderstood you:

png("base.png",width=400,height=600)
group=hclust(dist(mtcars))
plot(group, hang=-1,cex=0.7)
rect.hclust(group, k=3, border="red")
dev.off()

enter image description here

With ggdendro, drawing the rectangles is not trivial, but you can try using the color labeling:

library(ggdendro)
library(ggplot2)
hcdata <- dendro_data(group)
cluster_id <- cutree(group,3)[as.character(hcdata$labels$label)]
cluster_cols <- c("#b590ca","#a8d3da","#f5cab3")

ggplot() +
geom_segment(data=hcdata$segments, aes(x=x, y=y, xend=xend, yend=yend)) +
scale_x_continuous(breaks = seq_along(hcdata$labels$label), 
            labels = hcdata$labels$label)+
theme_dendro()+
theme(axis.text.x = element_text(angle = 90, 
hjust = 1,colour=cluster_cols[cluster_id])) + 
theme(axis.text.y = element_text(angle = 90, hjust = 1))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Hello, StupidWolf! Thank you very much for your attention and help! I tested ggdendro, but the results were very close to rect.hclust yet. And there was also the question of the separation of clusters... Finally, I found a good solution with the fviz_dend function of the factoextra package: fviz_dend(group, k = 3, cex = 0.9, lwd = 0.75, color_labels_by_k = FALSE, rect = TRUE) – Du Cerqueira Mar 09 '20 at 03:54
  • Next time provide your data. I still have no idea what works and what doesn't. Glad u got it going snyway – StupidWolf Mar 09 '20 at 07:03