熱圖可以聚合大量的數(shù)據(jù)扣囊,并可以用一種漸進(jìn)色來優(yōu)雅地表現(xiàn),可以很直觀地展現(xiàn)數(shù)據(jù)的疏密程度或頻率高低阀坏。
本文利用R語(yǔ)言?pheatmap包從頭開始繪制各種漂亮的熱圖如暖。參數(shù)像積木,拼湊出你最喜歡的熱圖即可忌堂,如下圖:
?????基因和樣本都可以單獨(dú)聚類盒至,排序,聚類再分組,行列注釋枷遂,配色調(diào)整樱衷,調(diào)整聚類線以及單元格的寬度和高度均可實(shí)現(xiàn)。
載入數(shù)據(jù)酒唉,R包
#R包library(pheatmap)# 構(gòu)建測(cè)試數(shù)據(jù) set.seed(1234)test = matrix(rnorm(200),20,10)test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] +3test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] +2test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] +4colnames(test) = paste("Test",1:10, sep ="")rownames(test) = paste("Gene",1:20, sep ="")head(test[,1:6])
繪制熱圖
繪制默認(rèn)熱圖
pheatmap(test)
基本參數(shù)
# scale = "row"參數(shù)對(duì)行進(jìn)行歸一化
# clustering_method參數(shù)設(shè)定不同聚類方法矩桂,默認(rèn)為"complete",可以設(shè)定為'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
pheatmap(test,scale ="row", clustering_method ="average")
#表示行聚類使用皮爾森相關(guān)系數(shù)聚類,默認(rèn)為歐氏距離"euclidean"
pheatmap(test, scale ="row", clustering_distance_rows ="correlation")
#行 列是否聚類痪伦,cluster_row ,cluster_col
pheatmap(test, cluster_row =FALSE,cluster_col =TRUE)
# treeheight_row和treeheight_col參數(shù)設(shè)定行和列聚類樹的高度侄榴,默認(rèn)為50
pheatmap(test, treeheight_row = 30, treeheight_col = 50)
# 設(shè)定cell?的大小
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)
設(shè)定 text
熱圖中展示數(shù)值
# display_numbers = TRUE參數(shù)設(shè)定在每個(gè)熱圖格子中顯示相應(yīng)的數(shù)值,#number_color參數(shù)設(shè)置數(shù)值字體的顏色
pheatmap(test, display_numbers = TRUE,number_color ="blue")
# 設(shè)定數(shù)值的顯示格式
pheatmap(test, display_numbers = TRUE, number_format ="%.1e")
#設(shè)定條件式展示
pheatmap(test, display_numbers = matrix(ifelse(test> 5,"*",""), nrow(test)))
設(shè)置 legend?
設(shè)定legend展示的值
#legend_breaks參數(shù)設(shè)定圖例顯示范圍网沾,legend_labels參數(shù)添加圖例標(biāo)簽
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0","1e-4","1e-3","1e-2","1e-1","1"))
#去掉legend
pheatmap(test, legend = FALSE)
設(shè)定 color
自定義顏色
#colorRampPalette
pheatmap(test, color = colorRampPalette(c("navy","white","firebrick3"))(50))
# border_color參數(shù)設(shè)定每個(gè)熱圖格子的邊框色
# border=TRIUE/FALSE參數(shù)是否要邊框線
pheatmap(test, border_color ="red", border=TRUE)
設(shè)定 annotations
# 生成行 列的注釋
annotation_col = data.frame( CellType = factor(rep(c("CT1","CT2"), 5)), Time = 1:5 )rownames(annotation_col) = paste("Test", 1:10, sep ="")annotation_row = data.frame( GeneClass = factor(rep(c("Path1","Path2","Path3"), c(10, 4, 6))))rownames(annotation_row) = paste("Gene", 1:20, sep ="")
#添加列的注釋
pheatmap(test, annotation_col = annotation_col)
#添加行 列的注釋
#angle_col?改變列標(biāo)簽的角度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col ="45")
# 根據(jù)聚類結(jié)果癞蚕,自定義注釋分組及顏色
ann_colors = list(Time=c("white","firebrick"),CellType=c(CT1="#1B9E77",CT2="#D95F02"),GeneClass=c(Path1="#7570B3",Path2="#E7298A",Path3="#66A61E") )pheatmap(test, annotation_col = annotation_col,annotation_row=annotation_row, annotation_colors = ann_colors, main ="Title")
設(shè)定 gap
#根據(jù)聚類結(jié)果,設(shè)定行g(shù)ap
pheatmap(test,?annotation_col =?annotation_col,?cluster_rows =?FALSE,?gaps_row =?c(10,?14))
#根據(jù)聚類結(jié)果辉哥,設(shè)定列g(shù)ap
pheatmap(test,annotation_col = annotation_col, cluster_rows = FALSE,cutree_col = 2)
#展示行或者列的label
labels_row =?c("",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"",?"Il10",?"Il15",?"Il1b")
pheatmap(test,?annotation_col =?annotation_col,?labels_row =?labels_row)
熱圖匯總
pheatmap(test,?annotation_col?=?annotation_col,?annotation_row?=?annotation_row,?annotation_colors?=?ann_colors,gaps_row?=?c(10,?14),cutree_col?=?2,main?=?"Pheatmap")
輸出結(jié)果
A = pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main ="Pheatmap")#記錄熱圖的行排序order_row = A$tree_row$order #記錄熱圖的列排序order_col = A$tree_col$order? # 按照熱圖的順序桦山,重新排原始數(shù)據(jù)result = data.frame(test[order_row,order_col])? # 將行名加到表格數(shù)據(jù)中result?=?data.frame(rownames(result),result,check.names?=F)??colnames(result)[1]?="geneid"#result結(jié)果按照熱圖中的順序write.table(result,file="reorder.txt",row.names=FALSE,quote?=?FALSE,sep='\t')
R的當(dāng)前工作目錄下即可查看熱圖的結(jié)果。
【公眾號(hào)對(duì)話框醋旦,回復(fù) R熱圖?即可獲得上述熱圖R代碼】
更多關(guān)于生信恒水,R,Python的內(nèi)容請(qǐng)掃碼關(guān)注小號(hào)饲齐,謝謝钉凌。