horizontal dendrogram in R with labels

后端 未结 2 2063
耶瑟儿~
耶瑟儿~ 2020-11-30 02:08

I am trying to draw a dendrogram from the hclust function output. I hope the dendrogram is horizontally arranged instead of the default, which can be obtain by

2条回答
  •  粉色の甜心
    2020-11-30 02:52

    Using dendrapply you can customize your dendro as you like.

    enter image description here

    colLab <- function(n) {
      if(is.leaf(n)) {
        a <- attributes(n)
        attr(n, "label") <- substr(a$label,1,2)             #  change the node label 
        attr(n, "nodePar") <- c(a$nodePar, lab.col = 'red') #   change the node color
      }
      n
    }
    
    require(graphics)
    hc <- hclust(dist(USArrests), "ave")
    clusDendro <- as.dendrogram(hc)
    clusDendro <- dendrapply(clusDendro, colLab)
    op <- par(mar = par("mar") + c(0,0,0,2))
    plot(clusDendro,horiz=T)
    

提交回复
热议问题