You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

dendextend包:指定标签分支着色失效问题求助

解决dendextend树状图指定标签分支着色问题

你的代码核心问题是调用set("by_labels_branches_col")时参数名错误——应该用labels指定目标标签,而非value。以下是修正后的两种可行方案:

方案一:修正参数名直接使用by_labels_branches_col

library(dendextend)
hc <- hclust(dist(USArrests))
dend <- as.dendrogram(hc)

# 为指定标签的分支着色
test <- dend %>% 
  set("by_labels_branches_col", labels = "Alabama", type = "any", col = "red") %>% 
  set("by_labels_branches_col", labels = "Georgia", type = "any", col = "green")

# 绘制树状图
plot(test, main = "Dendrogram with Alabama (Red) and Georgia (Green)")

方案二:通过分支索引设置颜色

先通过branches_attr_by_labels定位目标标签对应的分支,再为这些分支设置颜色:

library(dendextend)
hc <- hclust(dist(USArrests))
dend <- as.dendrogram(hc)

# 定位分支并着色
test <- dend %>%
  set("branches_col", branches_attr_by_labels(., labels = "Alabama"), "red") %>%
  set("branches_col", branches_attr_by_labels(., labels = "Georgia"), "green")

# 绘制树状图
plot(test, main = "Dendrogram with Alabama (Red) and Georgia (Green)")

两种方案都能实现将"Alabama"分支设为红色、"Georgia"分支设为绿色的需求。

内容的提问来源于stack exchange,提问作者kwasa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 17:38:24