熱圖——pheatmap學(xué)習(xí)
代碼及參數(shù)如下:
rm(list = ls())
options(stringsAsFactors = F)
#####heatmap####
#加載R包
library(pheatmap)
example("pheatmap")
# 構(gòu)建測(cè)試數(shù)據(jù)
set.seed(1234)
test = matrix(rnorm(200),nrow = 20,ncol = 10) #構(gòu)建測(cè)試矩陣
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 #前十行奇數(shù)列數(shù)據(jù)加3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2 #后10行偶數(shù)列加2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4 ##15-20行,偶數(shù)列數(shù)據(jù)再加4
colnames(test) = paste("Test", 1:10, sep = "") #添加行列名稱
rownames(test) = paste("Gene", 1:20, sep = "")
head(test[,1:6]) #查看1-6列的數(shù)據(jù)
## 繪制默認(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 = "complete")
#表示行聚類使用皮爾森相關(guān)系數(shù)聚類,默認(rèn)為歐氏距離"euclidean"
pheatmap(test,scale = "row",clustering_distance_rows ="euclidean")
#行 列是否聚類,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 = 12)
###設(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)
# 自定義顏色
#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 (cutree_col = 2)
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)
# 熱圖匯總 annotation_legend=F
# fontsize_row=10,fontsize_col = 12 行列標(biāo)簽字體大小
# fontsize_number = 10, cell內(nèi)部字體大小
pheatmap(test, scale = 'row',annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors,gaps_row = c(10, 14),
cutree_col = 2,main = "Pheatmap",angle_col = "45",
fontsize_row=10,fontsize_col = 12,
annotation_legend=T,drop_levels=T,fontsize_number = 10,
display_numbers = matrix(ifelse(test > 5, "*", ""),
nrow(test)),width = 100,height =200
)
僅展示熱圖匯總得結(jié)果!

image.png