Colorize Clusters in Dendogram with ggplot2

后端 未结 4 2015
不思量自难忘°
不思量自难忘° 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:36

    This approach is very similar to @DidzisElferts', just a little simpler.

    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
    
    library(ggplot2)
    library(ggdendro)
    hc       <- hclust(dist(df), "ave")           # heirarchal clustering
    dendr    <- dendro_data(hc, type="rectangle") # convert for ggplot
    clust    <- cutree(hc,k=2)                    # find 2 clusters
    clust.df <- data.frame(label=names(clust), cluster=factor(clust))
    # dendr[["labels"]] has the labels, merge with clust.df based on label column
    dendr[["labels"]] <- merge(dendr[["labels"]],clust.df, by="label")
    # plot the dendrogram; note use of color=cluster in geom_text(...)
    ggplot() + 
      geom_segment(data=segment(dendr), aes(x=x, y=y, xend=xend, yend=yend)) + 
      geom_text(data=label(dendr), aes(x, y, label=label, hjust=0, color=cluster), 
               size=3) +
      coord_flip() + scale_y_reverse(expand=c(0.2, 0)) + 
      theme(axis.line.y=element_blank(),
            axis.ticks.y=element_blank(),
            axis.text.y=element_blank(),
            axis.title.y=element_blank(),
            panel.background=element_rect(fill="white"),
            panel.grid=element_blank())
    

提交回复
热议问题