看帮助文档学热图

生成测试数据


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)
9475888-d6c23c441a7e1d5f.png
#忽略行(即基因)间的差异,专注列(样本)之间的差异
pheatmap(test, scale = "row")
9475888-309c418953e4afc9.png
#换颜色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
9475888-e3713f16bae62f4a.png
#不按行聚类
pheatmap(test, cluster_row = FALSE)
9475888-3863aaeb66a80b44.png
#不显示图例
pheatmap(test, legend = FALSE)
9475888-625de4fcdd001ded.png
# 图上显示文本,默认显示原数值
pheatmap(test, display_numbers = TRUE)
9475888-25d22f7ce5611f67.png
#科学计数法
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
9475888-5c131c2c312e7e43.png
#数值>5的标星号
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
9475888-5920531817e7f530.png
#添加标题,修改大小,存为pdf
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
9475888-0f64113754af6a58.png
#pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")

# 行列注释,格式是数据框,数据类型是因子。
#列,分组信息,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)
9475888-02de5419ef24d94b.png
#注释栏图例可不显示
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
9475888-c210b80471c884f0.png
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
9475888-9a0d758f984f5974.png

# 改变注释栏颜色
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")
9475888-5d77713ec00ce958.png
pheatmap(test, 
         annotation_col = annotation_col, 
         annotation_row = annotation_row, 
         annotation_colors = ann_colors)
9475888-0e1975b393a8fad6.png
pheatmap(test, 
         annotation_col = annotation_col, 
         annotation_colors = ann_colors[2]) 
9475888-d9cb7d03250cf2aa.png
# 显示gaps
pheatmap(test, 
         annotation_col = annotation_col, 
         cluster_rows = FALSE, 
         gaps_row = c(10, 14))
9475888-8c8dcc0f118fb4cb.png
pheatmap(test, 
         annotation_col = annotation_col, 
         cluster_rows = FALSE, 
         gaps_row = c(10, 14), 
         cutree_col = 2)
9475888-9729323040fccd8f.png
#需要指定行不排序才能加gaps,列同理。分簇后也加gaps用cutree_col和cutree_row,如果没有聚成簇,则此参数被忽略。
# 修改行列名,不显示用“”。
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
9475888-e691df296be433a0.png

猜你喜欢

转载自blog.csdn.net/weixin_33832340/article/details/87508656