R绘图_热图包pheatmap

pheatmap,全称Pretty Heatmaps,直译“完美的热图”,是一款非常优秀的热图绘制R包。网上也有很多可供参考的教程[1][2],在初学R后,我也曾写过了一篇教程[3][4],但是对于形形色色的绘图参数,存在堆砌罗列,无章可循,使之不利于后来者学习。

  • 官方示例

根据对pheatmap pdf说明文档[5]示例的解读,参考其他网络教程,加以归纳整理,确定了绘制热图的基本元素和绘图框架。

  • 构建数据集:创建数据、设置行名、列名
  • 绘图参数:颜色、边框线、图例、热图块大小、标签字体大小、热图块文本、字符格式及颜色、自定义行/列名、标签方向、标题
  • 构建注释参数:注释数据框、图例、注释颜色
  • 聚类相关参数:归一化、聚类数目、行列聚类、聚类树高度、添加gap、分块聚类、聚类方法、自定义聚类

后面参数教程绘图代码也是按照此流程、框架来进行演示:
由于图片较多,故只列出部分图片,其余可按代码运行依次生成

1. 构建数据集

rm(list = ls())
library(pheatmap)
#############构建数据集###############
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
# 设置行名、列名
rownames(test) <-  paste("Gene", 1:20, sep = "")
colnames(test) <-  paste("Test", 1:10, sep = "")

2. 绘图参数

# 默认绘图
pheatmap(test)
# 自定义热图颜色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
# 去掉热块边框线
pheatmap(test,  color = colorRampPalette(c("navy", "white", "firebrick3"))(50), 
         border = FALSE)
# 设定边框线色
pheatmap(test,  border_color = "red")
# 不绘制图例
pheatmap(test, legend = FALSE)
# legend_breaks参数设定图例断点范围,legend_labels参数添加图例断点标签
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, 
         legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1"))

图片:Rplot01---Rplot06

  • Rplot02.png
# 设置热图块大小
pheatmap(test, cellwidth = 15, cellheight = 12)
#fontsize参数设定标签字体大小
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8)

# 热图块内显示数值
pheatmap(test, display_numbers = TRUE)
# 设定数值字符串的格式及颜色
pheatmap(test, display_numbers = TRUE, number_format = "%.1e",number_color = "red")
# 构建矩阵文本,填充热图块
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
# 等同
pheatmap(test, display_numbers = ifelse(test > 5, "*", ""))

ifelse(test > 5, "", "") test矩阵内数字显著性判定
nrow(test) test矩阵内行数
matrix(ifelse(test > 5, "
", ""), nrow(test))) 将标记显著性的矩阵构建矩阵,行数为test行数

图片:Rplot07---Rplot012

  • Rplot07.png
  • Rplot09.png
  • Rplot11.png
# 参数设定是否显示行名和列名
pheatmap(test,show_rownames = F,show_colnames = F)
# 自定义行标签名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")
pheatmap(test, labels_row = labels_row)
# 设定列标签的方向
pheatmap(test, angle_col = "45")
pheatmap(test, angle_col = "0")
# 设置标题
pheatmap(test,  main = "The Pretty Heatmaps by myself")

图片:Rplot13---Rplot18

  • Rplot17.png

3. 构建注释参数

############构建注释参数###############
# 构建列注释数据框
annotation_col <-  data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)), 
  Time = 1:5
)
# 注释数据框'列名'必须和绘图矩阵'列名'一致
rownames(annotation_col) = paste("Test", 1:10, sep = "")
head(annotation_col)
# 构建行注释数据框
annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
#注释数据框'行名'必须和绘图矩阵'行名'一致
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
head(annotation_row)
# 显示列注释数据框
pheatmap(test, annotation_col = annotation_col)
# 显示列注释数据框和行注释数据框
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
# 取消注释的图例
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,
         annotation_legend = FALSE)

图片:Rplot19---Rplot21

  • Rplot19.png
  • Rplot20.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")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, 
         annotation_colors = ann_colors)
#只指定cellType颜色
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) 

图片:Rplot22---Rplot24

  • Rplot23.png

4. 聚类参数

############聚类参数###############
# 参数对行进行归一化(中心化)
pheatmap(test, scale = "row")
#聚类数目设定为4
pheatmap(test, kmeans_k = 4)
# 不对行进行聚类
pheatmap(test, cluster_row = FALSE)
#参数设定行和列聚类树的高度,默认为50
pheatmap(test, treeheight_row = 30, treeheight_col = 50)
# 取消按行聚类,在第10和14行处添加gap
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)

图片:Rplot25---Rplot30

  • Rplot29.png
  • Rplot30.png
# 设定不同聚类方法
pheatmap(test,scale = "row", clustering_method = "average")
# 设定聚类距离的方法
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

# 自定义聚类的距离方法
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
# 修饰聚类的回调函数
callback = function(hc, mat){
  sv = svd(t(mat))$v[,1]
  dend = reorder(as.dendrogram(hc), wts = sv)
  as.hclust(dend)
}
pheatmap(test, clustering_callback = callback)

图片:Rplot31---Rplot34

效果图:
按照数据-绘图参数-注释参数-聚类参数的顺序,思路清晰,可读性强,亦便于后续修改。

#按照数据-绘图参数-注释参数-聚类参数的顺序,思路清晰,可读性强,亦便于后续修改
pheatmap(test,
         color = colorRampPalette(c("navy","white","firebrick3"))(256),
         cellwidth = 20, cellheight = 20, border_color = "grey60", fontsize = 8, 
         display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)),
         main = "The Pretty Heatmaps By Myself",
         annotation_col = annotation_col,annotation_row = annotation_row, 
         annotation_colors = ann_colors,
         scale = "row", treeheight_row = 20, treeheight_col =30,
         cutree_cols = 2, cutree_rows = 2,
         clustering_method = "average")
  • Rplot35.png

参考资料:


  1. 使用pheatmap包绘制热图

  2. 热图最佳实践-pheatmap

  3. R包pheatmap绘制热图

  4. 使用Pheatmap包绘制差异miRNA热图

  5. https://CRAN.R-project.org/package=pheatmap</samp>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355