此post旨在提取heatmap作圖中的cluster行名或者列名
其實(shí),對heatmap熱圖提取cluster就是對hclust的結(jié)果提取cluster票从,tree_col和tree_row屬性
首先,我們要構(gòu)建一個(gè)matrix或者data.frame進(jìn)行pheatmap作圖
# package library
library(pheatmap)
library(tidyverse)
# Create random data
set.seed(50)
data <- replicate(20, rnorm(50, mean = 100, sd = 100))
data %>% class()
rownames(data) <- paste("Gene", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))
# get heatmap plot
(out <- pheatmap(data, scale="row"))
dev.copy(png, "heatmap_raw.png", width=4000, height=4000, res = 330)
dev.off()
[圖片上傳失敗...(image-52c3bd-1651998762319)]
隨后濒旦,我們提取熱圖中的行名和列名
# Re-order original data (genes) to match ordering in heatmap (top-to-bottom)
data[out$tree_row[["order"]],] %>% rownames()
# Re-order original data (samples) to match ordering in heatmap (left-to-right)
data[out$tree_row[["order"]],] %>% colnames()
# get hclust plot
plot(out$tree_col)
abline(h = 11.5, col="red", lty=2, lwd=2)
dev.copy(png, "heatmap_hclust.png", width=4000, height=4000, res = 330)
dev.off()
[圖片上傳失敗...(image-3b19fa-1651998649328)]
假如我們需要可視化的話误墓,可以加一個(gè)annotation
# get 3 cluster for sample name
(tree <- cutree(out$tree_col, k=3))
tree <- ifelse(tree == 1, "cluster_1",
ifelse(tree == 2, "cluster_2",
"cluster_3")) %>% as.data.frame() %>% setNames("Cluster")
# get annotation plot
pheatmap(data, scale = "row", annotation_col = tree)
dev.copy(png, "heatmap_3cluster.png", width=4000, height=4000, res = 330)
dev.off()
[圖片上傳失敗...(image-837391-1651998607583)]