This is a similar question but not quite the same since I would like to do this with dendextend. My issue is that some of the names of my labels are quite long and I would like to just remove them. I had tried a few techniques already.
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" ))
You can use dendrapply to change or remove the label attribute of the leaf nodes:
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" ))
Created on 2020-08-05 by the reprex package (v0.3.0)
Edit
As alternatives, you can always truncate the rownames and/or make some space in the plot and shift the bar down:
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)
Created on 2020-08-05 by the reprex package (v0.3.0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.