我可以用下面的代码生成一个绘图树:
library(factoextra)
library(plotly)
hcl <- hclust(
dist(mtcars, method = "euclidean"),
method = "complete")
ggplotly(
fviz_dend(
hcl,
k = 5,
show_labels = FALSE,
type = "phylogenic",
phylo_layout = "layout_as_tree"
)
)fviz_dend有一个显示标签的选项,但是当有成百上千的点时,这就变得丑陋了。相反,我希望能够将鼠标悬停在这一点上,并查看存储在hcl$labels中的标签。目前,悬停仅显示坐标和颜色代码:

发布于 2018-12-24 12:59:19
我不经常使用plotly包&希望有人能提供一个更优雅的解决方案,但是下面的方法(无论如何在我的机器上)可以处理系统树:
k = 5 # change this based on number of groups for cutting the tree
gp <- ggplotly(
fviz_dend(
hcl,
k = k,
show_labels = TRUE, # leave this as the default TRUE, so that the labels
# are passed to the plotly object; we can remove them
# in the next step.
type = "phylogenic",
phylo_layout = "layout_as_tree")
)
# remove hover text for line segments
gp$x$data[[1]]$text <- NA
# for each group, assign the label's text value to the point's text value,
# then remove the label
for(i in seq(k, 1)){
gp$x$data[[i+1]]$text <- gp$x$data[[i+1+k]]$text
gp$x$data[[i + 1 + k]] <- NULL
}
gp

注意:由矩形树创建的特定层数与上面的不同,因此系统发育树的解决方案不会直接适用。但我假设你的用例是针对系统进化的。
https://stackoverflow.com/questions/53908078
复制相似问题