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
相关产品推荐
相关产品推荐

