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)] + 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 = "")
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 ...
随机推荐
- Python-基本数据类型(list,tuple)
一. 列列表 1.1 列列表的介绍 列表是python的基础数据类型之⼀一,其他编程语言也有类似的数据类型. 比如JS中的数 组, java中的数组等等. 它是以[ ]括起来, 每个元素用' , ...
- 03- 基本的SQL语句介绍
01 库的操作新增库create database db1 charset utf8; # 由于在my.ini中已经配置了字符集,所以,charset utf8可以不写 查库# 查看当前创建的数据库s ...
- 常用的HTTP状态代码(4xx、5xx)详解
HTTP状态代码常用的如下: 400 无法解析此请求. 401.1 未经授权:访问由于凭据无效被拒绝. 401.2 未经授权: 访问由于服务器配置倾向使用替代身份验证方法而被拒绝. 401.3 未经授 ...
- 【入门】WebRTC知识点概览 | 内有技术干货免费下载
什么是WebRTC WebRTC 即Web Real-Time Communication(网页实时通信)的缩写,是一个支持网页浏览器之间进行实时数据传输(包括音频.视频.数据流)的技术.经过多年的发 ...
- jQuery-ajax-.load方法
使用jQuery封装的ajax是非常好用的,这个里面提供了几个比较好用的方法. load(url[,data, callback])方法: 说明:这个是jQuery中的最底层方法$.ajax()封装的 ...
- .NetCore中三种注入方式的思考
该篇内容由个人博客点击跳转同步更新!转载请注明出处! .NetCore彻底诠释了"万物皆可注入"这句话的含义,在.NetCore中到处可见注入的使用.因此core中也提供了三种注入 ...
- CentOS 常用命令合集
tail -f ../logs/catalina.out 在Tomcat中的bin目录下查看Tomcat日志 ps -ef|grep java 查看Tomcat服 ...
- 用jQuery做定位元素,做自动化测试你尝试过吗
一.前言 元素定位可以说是学自动化测试中必会技能之一,也可以说是通往自动化之路的开门钥匙. 就元素定位方法,除了我们常用并熟知的8种元素定位方法之外,还有一种定位方法可以说是一种特殊的存在,那就是JQ ...
- 基于SpringCloud的Microservices架构实战案例-架构拆解
自第一篇< 基于SpringCloud的Microservices架构实战案例-序篇>发表出来后,差不多有半年时间了,一直也没有接着拆分完,有如读本书一样,也是需要契机的,还是要把未完成的 ...
- 数据库系统概念:JDBC
import java.sql.*; public class DataBase { public static void main() { } } /* 5.1.1 JDBC */ class JD ...