【问题标题】:Adjustments to the graph of a cluster dendrogram对聚类树状图的调整
【发布时间】:2020-06-20 01:28:37
【问题描述】:

我在生成聚类树状图时遇到了一点问题。

集群技术正在发挥作用,问题只是图表。我想对它做些小调整。

X 轴上的名称与群组划分的线条末端之间的距离很大,并且不必要地覆盖了图表的大部分,我非常想减少它(图像上的绿色标记)。因此,我希望 Y 轴上的刻度增加(图像上的蓝色标记)。

有人知道如何解决这个问题吗?我搜索了 rect.hclust 的文档,但没有发现进行这些调整的参数。

为了便于理解,我附上了生成的树状图的脚本和图像以及我希望发生的事情。

非常感谢您的关注(和帮助)!

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")

【问题讨论】:

  • 情节(组,挂=-1); rect.hclust(group, k=3, border="red") 是正确的,您只需要调整绘图设备的大小,或者将其保存为 png

标签: r plot hierarchical-clustering dendrogram


【解决方案1】:

我认为 hang=-1 有效,除非我误解了你:

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()

使用ggdendro,绘制矩形并非易事,但您可以尝试使用颜色标签:

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))

【讨论】:

  • 你好,笨狼!非常感谢您的关注和帮助!我测试了ggdendro,但结果非常接近rect.hclust。还有集群分离的问题……最后,我找到了一个很好的解决方案,使用factoextra包的fviz_dend函数:fviz_dend(group, k = 3, cex = 0.9, lwd = 0.75, color_labels_by_k = FALSE,矩形 = TRUE)
  • 下次提供您的数据。我仍然不知道什么有效,什么无效。很高兴你能顺风顺水
猜你喜欢
  • 1970-01-01
  • 2018-03-06
  • 2012-06-27
  • 2021-11-06
  • 2013-03-01
  • 2021-06-07
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多