當(dāng)我們畫熱圖(heatmap)的時(shí)候,如果基因的名字比較多的時(shí)候姆钉,在熱圖中展示的話,就會(huì)出現(xiàn)名字重疊的情況潮瓶,針對(duì)這個(gè)情況陶冷,我們有兩種比較好的解決辦法毯辅,第一是在熱圖中僅顯示關(guān)注的基因名,其余基因名不顯示思恐;第二種方法是畫環(huán)狀熱圖。今天以第一種熱圖為例胀莹,來(lái)教小伙伴們?nèi)绾螌?shí)現(xiàn)基跑。
話不多數(shù)描焰,直接上代碼:
- 首選安裝依賴的程序包,后面繪圖要用到荆秦。
# 安裝包
if (!requireNamespace("BiocManager", quietly = TRUE)){install.packages("BiocManager")}
if (!requireNamespace("ComplexHeatmap", quietly = TRUE)){BiocManager::install("ComplexHeatmap")}
if (!requireNamespace("pals", quietly = TRUE)){BiocManager::install("pals")}
if (!requireNamespace("openxlsx", quietly = TRUE)){BiocManager::install("openxlsx")}
if (!requireNamespace("dplyr", quietly = TRUE)){BiocManager::install("dplyr")}
2 .程序包安裝好后,加載程序包
# 加載包
library(ComplexHeatmap)
library(openxlsx)
library(dplyr)
library(pals)
3.讀取繪圖用到的數(shù)據(jù)步绸,我們的數(shù)據(jù)格式行為基因名,列為樣本
# 讀入數(shù)據(jù),并歸一化(按照行歸一化)
# 因?yàn)閟cale()函數(shù)默認(rèn)是對(duì)矩陣按列標(biāo)準(zhǔn)化靡努,所以本次轉(zhuǎn)置了2次
df <- read.xlsx("heatmapIN.xlsx",rowNames = T) %>%
t() %>%
scale() %>%
t()
4.選擇熱圖中要顯示的基因名,可以選擇自己比較關(guān)注的基因惑朦。
# 指定要顯示的基因兽泄,以及確定基因的索引(位置)
gene.select <- c("KRT14","IGFBP2","LBP","PGAM1",
"LCAT","ITIH2", "ITIH1","PZP",
"HBD","KRT6A","APOA1",
"APOE","C4BPB")
pos <- which(rownames(df) %in% gene.select)
5.行注釋(即展示選擇的基因在熱圖中進(jìn)行展示)
# 行注釋
label <- rowAnnotation(
Zscore = anno_mark(at = pos,
labels = gene.select,
labels_gp = gpar(fontsize = 10),
lines_gp = gpar())
)
6.列注釋(我們用的數(shù)據(jù)列是樣本漾月,即對(duì)樣本的注釋信息)
# 列注釋
col.df <- read.xlsx("anno_df.xlsx",rowNames = T)
col_anno <- columnAnnotation(
Status = col.df[,1] %>% as.factor(), # 必須保證注釋的信息為因子水平,所以需要as.factor()
Class = col.df[,2] %>% as.factor(),
Group = col.df[,3] %>% as.factor()
)
7.繪圖,保存結(jié)果
p <- Heatmap(
df,
name = "Zscore",
row_names_gp = gpar(fontsize = 8),
rect_gp = gpar(col = "gray"),
cluster_rows = F,
cluster_columns = F,
show_column_names = F,
show_row_names = F,
right_annotation = label,
top_annotation = col_anno
)
png("heatmap.png", res = 100, width = 10, height = 8, units = 'in')
p
dev.off()
最終畫的熱圖效果如下: