Annotate the distance on nodes of a dendrograms

 ̄綄美尐妖づ 提交于 2019-12-12 17:19:07

问题


Consider a simple dendrogram like

dend <- 1:5 %>% dist %>% hclust %>% as.dendrogram

How can I annotate the distance (height) on the nodes? I looked up the dendextend package but it does not offer such a feature. However, I know I can the list of these distances from

heights <- as.list(dend %>% get_nodes_attr("height"))

Any help is highly appreciated.


回答1:


Here's a simple example using hc2axes from the pvclust package:

# install.packages("pvclust") # install package if needed
plot(hc <- hclust(dist( mtcars[1:10, ])))
with(pvclust:::hc2axes(hc), 
     text(x.axis, y.axis, round(y.axis, 2), adj = c(.5, 1))
)




回答2:


With the help from the comment one solution is:

heights <- get_nodes_attr(HC[[i]], "height")
heights <- as.list(sort(heights))
heights[which(heights ==0)] <- NULL
for (node in 1: length(heights)){
  with(pvclust:::hc2axes(as.hclust(dend)),
    text(x.axis[node], y.axis[node], pos = 1, sprintf("%.2f", heights[node]))) 
}


来源:https://stackoverflow.com/questions/36129111/annotate-the-distance-on-nodes-of-a-dendrograms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!