简体   繁体   中英

Remove labels from dendogram generated with dendextend

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.

  1. I tried changing cex = 0
  2. I tried changing the color to white but all this does is hide the labels and when the names are long is still an issue.
  3. I tried plotting labels =F, but this does not work as well. Is there anyway to completely eliminate the labels? Here is an example code.
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM