r - 从使用 dendextend 生成的树状图中删除标签
问题描述
这是一个类似的问题,但不完全相同,因为我想用dendextend 来做这个。我的问题是我的一些标签名称很长,我想删除它们。我已经尝试了一些技术。
- 我尝试更改 cex = 0
- 我尝试将颜色更改为白色,但这只是隐藏标签,而且当名称很长时仍然是一个问题。
- 我尝试绘制标签 =F ,但这也不起作用。反正有没有完全消除标签?这是一个示例代码。
dend <- USArrests[1:5, ] %>%
dist() %>%
hclust() %>%
as.dendrogram()
dend = dend%>% set("labels_cex", 0) %>% set("labels_col", "white") # change to white however this does not work well because the color bars would just get pushed out
dend = dend%>% set("labels_cex", 0) %>% set("labels_col", "black") # setting cex to 0 does nothing
plot(dend, labels=FALSE ) # labels =F are ignore
colored_bars(colors = cbind (
state= "red" ))
解决方案
您可以使用 dendrapply 更改或删除叶节点的标签属性:
suppressPackageStartupMessages(invisible(
lapply(c("dendextend", "dplyr"),
require, character.only = TRUE)))
dend <- USArrests[1:5, ] %>%
dist() %>%
hclust() %>%
as.dendrogram()
noLabel <- function(x) {
if (stats::is.leaf(x)) {
attr(x, "label") <- NULL }
return(x)
}
plot(stats::dendrapply(dend, noLabel))
colored_bars(colors = cbind (state= "red" ))
由reprex 包(v0.3.0)于 2020 年 8 月 5 日创建
编辑
作为替代方案,您始终可以截断行名和/或在图中留出一些空间并将条形向下移动:
suppressPackageStartupMessages(invisible(
lapply(c("dendextend", "dplyr", "stringr"),
require, character.only = TRUE)))
oldpar <- par()
par(mar=c(8,4,4,2))
dend <- data.frame(USArrests[1:5, ],
row.names = str_trunc(rownames(USArrests[1:5, ]), 8, ellipsis="..")) %>%
dist() %>%
hclust() %>%
as.dendrogram() %>% plot()
colored_bars(colors = cbind (state= "red" ), y_shift = -60)
par(mar=oldpar$mar)
由reprex 包(v0.3.0)于 2020 年 8 月 5 日创建
推荐阅读
- visual-studio - VS2019 中 ASP.NET Core 2.2 项目中的分析器警告
- java - HttpURLConnection:写入OutputStream,得到响应
- c# - 按球体上的位置对 Vector3s 进行排序
- java - BufferedReader 从 CMD processbuilder (JAVA) 读取输出的问题?
- javascript - 为什么我对 ipcMain 的回复按字母顺序排序?
- c++ - 如何编写带有模板函数参数的包装函数,它可以采用重载的成员函数?
- javascript - 从版本 2.3.0 到 4.3.0 的 Angular 迁移,mapValuesToArray 符号问题
- python - 将 Dash 用作 Figure 的错误作为传递给 Graph 的无效参数“figure.layout”,预期为“对象”。提供类型“数组”
- sitecore - 默认体验分析仪表板报告的自定义过滤
- java - 使用休眠返回查询的错误计数值