生成測(cè)試數(shù)據(jù)
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 9, 2)] = test[1:10, seq(1, 9, 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 = "")
畫熱圖
library(pheatmap)
pheatmap(test)

#忽略行(即基因)間的差異,專注列(樣本)之間的差異
pheatmap(test, scale = "row")

#換顏色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

#不按行聚類
pheatmap(test, cluster_row = FALSE)

#不顯示圖例
pheatmap(test, legend = FALSE)

# 圖上顯示文本,默認(rèn)顯示原數(shù)值
pheatmap(test, display_numbers = TRUE)

#科學(xué)計(jì)數(shù)法
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")

#數(shù)值>5的標(biāo)星號(hào)
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

#添加標(biāo)題,修改大小,存為pdf
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")

#pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
# 行列注釋,格式是數(shù)據(jù)框,數(shù)據(jù)類型是因子。
#列,分組信息,test1:10分別屬于CT1和CT2
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)),
Time = 1:5
)
rownames(annotation_col) = colnames(test)
annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = rownames(test)
# 顯示行列分組信息注釋欄
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)

# 改變注釋欄顏色
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_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
pheatmap(test,
annotation_col = annotation_col,
cluster_rows = FALSE,
gaps_row = c(10, 14))

pheatmap(test,
annotation_col = annotation_col,
cluster_rows = FALSE,
gaps_row = c(10, 14),
cutree_col = 2)

#需要指定行不排序才能加gaps,列同理。分簇后也加gaps用cutree_col和cutree_row,如果沒有聚成簇,則此參數(shù)被忽略。
# 修改行列名,不顯示用“”。
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)


微信公眾號(hào)生信星球同步更新我的文章,歡迎大家掃碼關(guān)注!

我們有為生信初學(xué)者準(zhǔn)備的學(xué)習(xí)小組,點(diǎn)擊查看??
想要參加我的線上線下課程,也可加好友咨詢??
如果需要提問(wèn),請(qǐng)先看生信星球答疑公告