pheatmap绘制“热图”,你需要的都在这
热图可以聚合大量的数据,并可以用一种渐进色来优雅地表现,可以很直观地展现数据的疏密程度或频率高低。
本文利用R语言 pheatmap 包从头开始绘制各种漂亮的热图。参数像积木,拼凑出你最喜欢的热图即可,如下图:
基因和样本都可以单独聚类,排序,聚类再分组,行列注释,配色调整,调整聚类线以及单元格的宽度和高度均可实现。
载入数据,R包
#R包library(pheatmap)# 构建测试数据set.seed(1234)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 = "")head(test[,1:6])
绘制热图
绘制默认热图
pheatmap(test)
基本参数
# scale = "row"参数对行进行归一化
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
pheatmap(test,scale = "row", clustering_method = "average")
#表示行聚类使用皮尔森相关系数聚类,默认为欧氏距离"euclidean"
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
#行 列是否聚类,cluster_row ,cluster_col
pheatmap(test, cluster_row = FALSE,cluster_col = TRUE)
# treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50
pheatmap(test, treeheight_row = 30, treeheight_col = 50)
# 设定cell 的大小
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)
设定 text
热图中展示数值
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,#number_color参数设置数值字体的颜色
pheatmap(test, display_numbers = TRUE,number_color = "blue")
# 设定数值的显示格式
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
#设定条件式展示
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
设置 legend
设定legend展示的值
#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"))
#去掉legend
pheatmap(test, legend = FALSE)
设定 color
自定义颜色
#colorRampPalette
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
# border_color参数设定每个热图格子的边框色
# border=TRIUE/FALSE参数是否要边框线
pheatmap(test, border_color = "red", border=TRUE)
设定 annotations
# 生成行 列的注释
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)
#添加行 列的注释
#angle_col 改变列标签的角度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
# 根据聚类结果,自定义注释分组及颜色
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, main = "Title")
设定 gap
#根据聚类结果,设定行gap
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
#根据聚类结果,设定列gap
pheatmap(test,annotation_col = annotation_col, cluster_rows = FALSE,cutree_col = 2)
#展示行或者列的label
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
热图汇总
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap")
输出结果
A = pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap") #记录热图的行排序order_row = A$tree_row$order#记录热图的列排序order_col = A$tree_col$order# 按照热图的顺序,重新排原始数据result = data.frame(test[order_row,order_col])# 将行名加到表格数据中result = data.frame(rownames(result),result,check.names =F)colnames(result)[1] = "geneid"#result结果按照热图中的顺序write.table(result,file="reorder.txt",row.names=FALSE,quote = FALSE,sep='\t')
R的当前工作目录下即可查看热图的结果。
【公众号对话框,回复 R热图 即可获得上述热图R代码】
更多关于生信,R,Python的内容请扫码关注小号,谢谢。
pheatmap绘制“热图”,你需要的都在这的更多相关文章
- MATLAB实例:求相关系数、绘制热图并找到强相关对
MATLAB实例:求相关系数.绘制热图并找到强相关对 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 用MATLAB编程,求给定数据不同维度之间的相关系 ...
- pheatmap() 的热图制作
1.数据准备 2.画图 3.参数调整 (转自百迈克公众号) 关注下方公众号可获得更多精彩
- 用R包中heatmap画热图
一:导入R包及需要画热图的数据 library(pheatmap) data<- read.table("F:/R练习/R测试数据/heatmapdata.txt",head ...
- [R] 如何绘制各样本的pathway丰度热图?
前言 一般而言,我们做完pathway富集分析,就做下气泡图或bar图来进行展示,但它们实际上只考虑了富集因子和Pvalue.如果我们不关注这两个因素,而是在乎样本本身的pathway丰度呢? 对于K ...
- pheatmap, gplots heatmap.2和ggplot2 geom_tile实现数据聚类和热图plot
主要步骤 pheatmap 数据处理成矩阵形式,给行名列名 用pheatmap画热图(pheatmap函数内部用hclustfun 进行聚类) ggplot2 数据处理成矩阵形式,给行名列名 hclu ...
- R语言学习 - 热图简化
绘制热图除了使用ggplot2,还可以有其它的包或函数,比如pheatmap::pheatmap (pheatmap包中的pheatmap函数).gplots::heatmap.2等. 相比于gg ...
- 扩增子图表解读3热图:差异菌、OTU及功能
热图是使用颜色来展示数值矩阵的图形.通常还会结合行.列的聚类分析,以表达实验数据多方面的结果. 热图在生物学领域应用广泛,尤其在高通量测序的结果展示中很流行,如样品-基因表达,样品-OTU相对丰度矩 ...
- gplots heatmap.2和ggplot2 geom_tile实现数据聚类和热图plot
主要步骤 ggplot2 数据处理成矩阵形式,给行名列名 hclust聚类,改变矩阵行列顺序为聚类后的顺序 melt数据,处理成ggplot2能够直接处理的数据结构,并加上列名 ggplot_tile ...
- 基于matplotlib的数据可视化 - 热图imshow
热图: Display an image on the axes. 可以用来比较两个矩阵的相似程度 mp.imshow(z, cmap=颜色映射,origin=垂直轴向) imshow( X, cma ...
随机推荐
- 节能减排到底如何----google earth engine 告诉你!!
(First,再次严谨说明,本人成果未经允许,切勿发表到相关学术期刊,如果有技术交流,qq1044625113,顺便打个广告,兼职GEE开发,欢迎联系!) 终于过了严寒的冬天,2017年的冬天中国南方 ...
- Scala 学习之路(十二)—— 类型参数
一.泛型 Scala支持类型参数化,使得我们能够编写泛型程序. 1.1 泛型类 Java中使用<>符号来包含定义的类型参数,Scala则使用[]. class Pair[T, S](val ...
- Mysql索引优化之索引的分类
Mysql的历史 简单回顾一下Mysql的历史,Mysql 是一个关系型数据库管理系统,由瑞典 Mysql AB 公司开发,目前属于 Oracle 公司.关系型数据库将数据保存在不同的表中,而不是将 ...
- js深入(四)万脸懵圈的this指向
作为一个js菜鸡的我而言,在之前讲到过那么多的js链式查找机制,比如说原型链,作用域链等等,想当然的把这个机制带入到了this指向上边,结果就是这个this指向指的我万脸懵逼(标题换字了,担心被河蟹) ...
- scikit-learn算法选择路径图
 原文链接:https://blog.csdn.net/guang_mang/article/details/73658496
- yarn or npm 版本固化如何选择
前言 作为前端开发者,npm这个包管理工具的重要性显而易见.优点不再表述,但一些缺点是为使用者诟病比较多的:速度慢.版本控制.下面主要讨论下npm的版本固化问题,即lock文件. npm语义化版本管理 ...
- 关于char[]转换成LPCWSTR的有关问题[转]
一.问题的原因:VS2010默认采用宽字符UNICODE编码方式,定义了Unicode,因此相关的字符串必须为unicode字符串,而非ascii字符串. LPCWSTR中的W是宽字符的意思,是UNI ...
- 源码阅读 - java.util.concurrent (三)ConcurrentHashMap
在java.util.concurrent包中提供了一个线程安全版本的Map类型数据结构:ConcurrentMap.本篇文章主要关注ConcurrentMap接口以及它的Hash版本的实现Concu ...
- 我的那些年(12)~公司技术转行,我也跟着转到java了
回到目录 我的那些年(12)~公司技术转行,我也跟着转到java了 CTO换人了 微软技术栈不被认可经常被喷 技术统一向java转 换了mac book后,docker还是很占内存 学习springb ...
- ajax 前端发含有列表的数据
在前端页面也可以给后端发送一个包含列表的数据 html <body> <h3>index页面 </h3> <input type="text&quo ...