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)!
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")
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()
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))
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.