糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > R语言绘制热图——pheatmap

R语言绘制热图——pheatmap

时间:2019-02-06 00:17:03

相关推荐

R语言绘制热图——pheatmap

(网易云课堂,腾讯课堂生物信息讲师,高级生物信息工程师)

pheatmap简介: Pretty Heatmaps——Implementation of heatmaps that offers more control over dimensions and appearance.

library(pheatmap)#创建数据集test测试矩阵test = matrix(rnorm(200), 20, 10)test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4colnames(test) = paste("Test", 1:10, sep = "")rownames(test) = paste("Gene", 1:20, sep = "")# 用pheatmap函数画热图pheatmap(test)

#默认参数下是对行列均进行聚类(可设置cluster_row = FALSE, cluster_col = FALSE不进行行列的聚类;如果进行聚类了,还可以通过设置treeheight_row=0, treeheight_col=0不显示dendrogram),矩阵没有进行标准化(标准化参数为scale,可选"none", "row", "column"),热图的每个小块之间以灰色隔开(参数border_color,如果不想要border可以设置为NA,当然也可以设置成其它颜色),legend显示在右上方(可设置legend = FALSE不显示legend);热图的颜色可利用参数color调整;

"1e-4", "1e-3", "1e-2", "1e-1", "1"))#可自己设置图例

#可设置参数display_numbers将数值显示在热图的格子中,可通过number_format设置数值的格式,较常用的有"%.2f"(保留小数点后两位),"%.1e"(科学计数法显示,保留小数点后一位),number_color设置显示内容的颜色:

pheatmap(test, display_numbers = TRUE, number_format = "%.2f", number_color="purple") #"%.2f"表示保留小数点后两位

# 在热图格子里展示文本pheatmap(test, display_numbers = TRUE)pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))#还可以自己设定要显示的内容;

#pheatmap参数设置改变每个格子的大小

pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap", fontsize = 8, filename = "test.pdf") #main可设置热图的标题,fontsize设置字体大小,filename可直接将热图存出,支持格式png, pdf, tiff, bmp, jpeg,并且可以通过width, height设置图片的大小;

#pheatmap还可以显示行或列的分组信息,支持多种分组;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, 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_row = annotation_row,annotation_colors = ann_colors)

#pheatmap还能够根据特定的条件将热图分隔开;

# cutree_rows, cutree_cols:根据行列的聚类数将热图分隔开;

pheatmap(test,cutree_rows=3,cutree_cols=2)

#还可以自己设定各个分组的颜色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)#还可以利用gaps_row, gaps_col自己设定要分隔开的位置pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),cutree_col = 2)

如果觉得《R语言绘制热图——pheatmap》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。