Colorize Clusters in Dendogram with ggplot2

后端 未结 4 2013
不思量自难忘°
不思量自难忘° 2020-12-14 08:55

Didzis Elferts showed how to plot a dendogram using ggplot2 and ggdendro:

horizontal dendrogram in R with labels

here is the code:

labs = pas         


        
4条回答
  •  星月不相逢
    2020-12-14 09:27

    A short way to achieve a similar result is to use the package dendextend, taken from this overview.

    df   <- USArrests   # really bad idea to muck up internal datasets
    labs <- paste("sta_",1:50,sep="") # new labels
    rownames(df) <- labs # set new row names
    
    require(magrittr)
    require(ggplot2)
    require(dendextend)
    
    dend <- df %>% dist %>%
      hclust %>% as.dendrogram %>%
      set("branches_k_color", k = 4) %>% set("branches_lwd", 0.7) %>%
      set("labels_cex", 0.6) %>% set("labels_colors", k = 4) %>%
      set("leaves_pch", 19) %>% set("leaves_cex", 0.5) 
    ggd1 <- as.ggdend(dend)
    ggplot(ggd1, horiz = TRUE)
    

    Note: The order of the states is slightly different compared to those above - not really changing interpretation though.

提交回复
热议问题