一:創建矩陣:
# Create test matrix
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
二:畫pheatmap圖
# Draw heatmaps
圖:1:pheatmap(test)? ##啥都不加?
圖2:pheatmap(test, kmeans_k = 2)? ?##分成2x的圖
圖3:pheatmap(test, scale = "row", clustering_distance_rows = "correlation")? ##clustering_distance_row表示行距離度量的方法?#表示行聚類使用皮爾森相關系數聚類。
圖4:pheatmap(test,color = colorRampPalette(c("navy", "white", "firebrick3"))(50))? ? ##賦值漸變顏色調色板colorRampPalette屬性,選擇“?深藍色,磚紅色,”漸變,50就是將這三種顏色設置為50個梯度一般看到的這三個"green","black","red")
圖5:pheatmap(test, cluster_row = FALSE)? ##我們不應該對行進行聚類? ?(##列的順序是時間序列,我們不應該對列進行聚類)
圖6:pheatmap(test,legend = FALSE)? ?##是否顯示圖例
三:顯示單元格內容
# Show text within cells
pheatmap(test, display_numbers = TRUE)
圖7:pheatmap(test, display_numbers = TRUE, number_format = "%.2f")##number_format設置數值的格式,較常用的有"%.2f"(保留小數點后兩位,1f,暴力小數點后1位),"%.1e"(科學計數法顯示,保留小數點后一位)
圖8:pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))##test的沒一行中大于5的用*標出來
圖9:pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"))##在legend上的-1~4的位置顯示'0', '1e-4', '1e-3', '1e-2', '1e-1', '1'
圖10:pheatmap(test, border_color = blues9, display_numbers = TRUE)##熱圖的每個小塊之間以灰色隔開(參數border_color,如果不想要border可以設置為blues9,當然也可以設置成其它顏色)
四:修正單元格大小,并將其保存到具有正確大小的文件中
# Fix cell sizes and save to file with correct size
圖11:pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")?#main可設置熱圖的標題
圖12:pheatmap(test, cellwidth = 15, cellheight = 12,fontsize = 8, filename = "test.pdf")?#fontsize設置字體大小,filename可直接將熱圖存出,支持格式png, pdf, tiff, bmp, jpeg,并且可以通過width(寬), height(高)設置圖片的 大小;
五:為行和列生成注釋并顯示顯示行和顏色注釋
# 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
圖13:pheatmap(test,annotation_col = annotation_col)? ##顯示行列注釋
圖14:pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)? ?##行與列的注釋分開
圖15:pheatmap(test, annotation_col = annotation_col,annotation_legend = FALSE)##右上角注釋圖例不現實
六:更改列中文本的角度
#Change angle of text in the columns
圖16:pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,angle_col = "45")##列下面的注釋角度為45度('arg' should be one of “270”, “0”, “45”, “90”, “315”)
圖17:pheatmap(test, annotation_col = annotation_col,angle_col = "0")??##列下面的注釋角度為0度
七:指定顏色
# Specify colors
ann_colors= list(
??Time = c("white", "firebrick"),
??CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E"))##注意ann_colors是列表
圖18:pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title")
圖19:pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors)
圖20:pheatmap(test, annotation_col = annotation_col, annotation_colors =ann_colors[2]) 2可以改變得? ?
圖21:pheatmap(test, annotation_col = annotation_col, annotation_colors =ann_colors[4])
八:生成有gap的熱圖
#Gaps in heatmaps?
圖22:pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))??
圖23:pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), cutree_col = 2)??#cutree_rows, cutree_cols可以根據行列的聚類數將熱圖分隔開
九:顯示自定義字符串作為行/列名稱
#Show custom strings as row/col names
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "","", "","Il10", "Il15", "Il1b")##這里相當于是給表達量最高?的三個命名了
圖24:pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
labels_row = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
???????????????"16", "17", "18", "19", "20")? ?##可以給所有的20行都命名(如第三幅圖)
圖25:pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)??
十:從距離矩陣指定聚類
# Specifying clustering from distance matrix
drows = dist(test, method = "minkowski")? ? #表示行聚類除了使用皮爾森相關系數聚類,也可以自定義為這種minkowski
dcols = dist(t(test), method = "minkowski")
圖26:pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
十一:使用群集回調選項修改群集的順序
#Modify ordering of the clusters using clustering callback option
callback = function(hc, mat){
??sv = svd(t(mat))$v[,1]
??dend = reorder(as.dendrogram(hc), wts = sv)
??as.hclust(dend)
}
圖27:pheatmap(test, clustering_callback = callback)
十二:十一這一步可以用dendsort包畫畫
library(dendsort)
callback = function(hc, ...){dendsort(hc)}
圖28:pheatmap(test, clustering_callback = callback)
搞定!