初學(xué)者R語言:熱圖基礎(chǔ)畫法及個性化調(diào)整詳解

熱圖(Heatmap):用顏色變化直觀的表達(dá)數(shù)據(jù)之間差異的圖,是對實驗數(shù)據(jù)進(jìn)行質(zhì)制和差異數(shù)據(jù)的展現(xiàn),是數(shù)據(jù)挖掘類文章的標(biāo)配。

Rplot1.jpeg

例如上圖,每個小方格表示每個基因,其顏色表示該基因表達(dá)量大小,表達(dá)量越大顏色越深(紅色為上調(diào),藍(lán)色為下調(diào))。每行表示每個基因在不同樣本中的表達(dá)量情況,每列表示每個樣品中所有基因的表達(dá)量情況。上方樹形圖表示對來自不同實驗分組的不同樣品的聚類分析結(jié)果

Draw heatmaps#畫熱圖

pheatmap(test)
pheatmap(test, kmeans_k = 2)#把所有行歸為兩類畫熱圖
pheatmap(test, scale = "row")#歸一化(按照行),防止因極大值使其他值的差異顯現(xiàn)不出來
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))#設(shè)定圖表顏色,不設(shè)定則為默認(rèn)顏色,50為50個色塊
pheatmap(test, cluster_row = FALSE)#不按照行排序,意味遵循表格原始行的順序
pheatmap(test, legend = FALSE)#不標(biāo)注注釋信息

Show text within cells#在單元格中顯示文本

pheatmap(test, display_numbers = TRUE)#顯示每個格子具體代表的數(shù)字
pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))#大于5的*號
pheatmap(test, cluster_row = FALSE,  legend_breaks= -1:4,)
)
legend_breaks= -1:4#只顯示注釋信息的-1到4,
legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1")#把注釋標(biāo)簽換成這幾個數(shù)字

Fix cell sizes and save to file with correct size#修復(fù)單元格大小并以正確的大小保存到文件

pheatmap(test, cellwidth = 15, cellheight = 12,main = "Example heatmap")#設(shè)定寬,高,main為設(shè)定表的表頭,也就是名字
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")#fontsize設(shè)定字體大小,filename設(shè)定文件名

Generate annotations for rows and columns#為行和列生成注釋

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 = "")

Display row and color annotations#顯示行和列注釋

pheatmap(test, annotation_col = annotation_col)#顯示列注釋
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)#顯示列注釋,并去掉其解說
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)#顯示行注釋及列注釋

Change angle of text in the columns更改列中文本的角度

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")#下標(biāo)文本傾斜45角書寫
pheatmap(test, annotation_col = annotation_col, angle_col = "0")#下標(biāo)文本水平書寫

Specify colors指定顏色

ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)

分別設(shè)定3種注釋不同的顏色

pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, 
         annotation_colors = ann_colors)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) 

把顏色顯示在圖表中

# Gaps in heatmaps
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))#在第10行和14行后分開
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), 
         cutree_col = 2)#縱分為兩部分

Show custom strings as row/col names將自定義字符串顯示為行/列名稱

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")#定義你想要的行名稱

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)#替換原來的行名稱

Specifying clustering from distance matrix從距離矩陣指定聚類

drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
pheatmap(test)
pheatmap(test, kmeans_k = 2)#把所有行歸為兩類畫熱圖
pheatmap(test, scale = "row")#歸一化(按照行),防止因極大值使其他值的差異顯現(xiàn)不出來
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))#設(shè)定圖表顏色,不設(shè)定則為默認(rèn)顏色,50為50個色塊
pheatmap(test, cluster_row = FALSE)#不按照行排序,意味遵循表格原始行的順序
pheatmap(test, legend = FALSE)#不標(biāo)注注釋信息

Show text within cells#在單元格中顯示文本

pheatmap(test, display_numbers = TRUE)#顯示每個格子具體代表的數(shù)字
pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))#大于5的*號
pheatmap(test, cluster_row = FALSE,  legend_breaks= -1:4,)
)
,#legend_breaks= -1:4#只顯示注釋信息的-1到4,
,#legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1")#把注釋標(biāo)簽換成這幾個數(shù)字

Fix cell sizes and save to file with correct size#修復(fù)單元格大小并以正確的大小保存到文件

pheatmap(test, cellwidth = 15, cellheight = 12,main = "Example heatmap")#設(shè)定寬,高,main為設(shè)定表的表頭,也就是名字
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")#fontsize設(shè)定字體大小,filename設(shè)定文件名

Generate annotations for rows and columns#為行和列生成注釋

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 = "")

Display row and color annotations#顯示行和列注釋

pheatmap(test, annotation_col = annotation_col)#顯示列注釋
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)#顯示列注釋,并去掉其解說
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)#顯示行注釋及列注釋

Change angle of text in the columns更改列中文本的角度

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")#下標(biāo)文本傾斜45角書寫
pheatmap(test, annotation_col = annotation_col, angle_col = "0")#下標(biāo)文本水平書寫

Specify colors指定顏色

ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)#分別設(shè)定3種注釋不同的顏色

pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, 
         annotation_colors = ann_colors)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) #把顏色顯示在圖表中

Gaps in heatmaps

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))#在第10行和14行后分開
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), 
         cutree_col = 2)#縱分為兩部分

Show custom strings as row/col names將自定義字符串顯示為行/列名稱

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")#定義你想要的行名稱

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)#替換原來的行名稱

Specifying clustering from distance matrix

drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容