LoginSignup
2
1

Pheatmapクラスタリング

Last updated at Posted at 2024-04-01

Rのpheatmapを用いた階層型クラスタリングのスクリプト
いつも忘れてしまうので備忘録として。

library(pheatmap)

ph <- pheatmap(mat, 
               cluster_cols = TRUE,
               cluster_rows = FALSE,
               show_colnames = FALSE,
               clustering_method = "ward.D")

#クラスタリング数を変える
clusters <- data.frame(x =matrix(nrow = ncol(data.frame(mat)), ncol = 1))
for(i in 2:7){
  tmp <- cutree(ph$tree_col, k = i)
  clusters <- cbind(clusters, tmp)
}
clusters$x <- NULL
colnames(clusters) <- paste0("ph_", 2:7)

#各グループ内をfactor型に変換する
for(i in 1:ncol(clusters)){
  clusters[,i] <- as.factor(clusters[,i])
}

#得られたクラスターをアノテーションとして可視化
ph <- pheatmap(mat, 
               cluster_cols = TRUE,
               cluster_rows = FALSE,
               show_colnames = FALSE,
               clustering_method = "ward.D",
               annotation_col = clusters
               )

覚えてしまいたいところ、、

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1