百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

R语言学习 - 热图简化

bigegpt 2024-09-17 12:38 5 浏览

热图绘制 - pheatmap

绘制热图除了使用ggplot2,还可以有其它的包或函数,比如pheatmap::pheatmap (pheatmap包中的pheatmap函数)、gplots::heatmap.2等。

相比于ggplot2作heatmap, pheatmap会更为简单一些,一个函数设置不同的参数,可以完成行列聚类、行列注释、Z-score计算、颜色自定义等。那我们来看看效果怎样。

data_ori <- "Grp_1;Grp_2;Grp_3;Grp_4;Grp_5
a;6.6;20.9;100.1;600.0;5.2
b;20.8;99.8;700.0;3.7;19.2
c;100.0;800.0;6.2;21.4;98.6
d;900;3.3;20.3;101.1;10000"
data <- read.table(text=data_ori, header=T, row.names=1, sep=";", quote="")
  Grp_1 Grp_2 Grp_3 Grp_4   Grp_5
a   6.6  20.9 100.1 600.0     5.2
b  20.8  99.8 700.0   3.7    19.2
c 100.0 800.0   6.2  21.4    98.6
d 900.0   3.3  20.3 101.1 10000.0
pheatmap::pheatmap(data, filename="pheatmap_1.pdf")

虽然有点丑,但一步就出来了。

heatmap美化篇提到的数据前期处理方式,都可以用于pheatmap的画图。此外Z-score计算在pheatmap中只要一个参数就可以实现。

pheatmap::pheatmap(data, scale="row", filename="pheatmap_1.pdf")

有时可能不需要行或列的聚类,原始展示就可以了。

pheatmap::pheatmap(data, scale="row", cluster_rows=FALSE, cluster_cols=FALSE, filename="pheatmap_1.pdf")

给矩阵 (data)中行和列不同的分组注释。假如有两个文件,第一个文件为行注释,其第一列与矩阵中的第一列内容相同 (顺序没有关系),其它列为第一列的不同的标记,如下面示例中(假设行为基因,列为样品)的2,3列对应基因的不同类型 (TF or enzyme)和不同分组。第二个文件为列注释,其第一列与矩阵中第一行内容相同,其它列则为样品的注释。

row_anno = data.frame(type=c("TF","Enzyme","Enzyme","TF"), class=c("clu1","clu1","clu2","clu2"), row.names=rownames(data))
row_anno
    type class
a     TF  clu1
b Enzyme  clu1
c Enzyme  clu2
d     TF  clu2
col_anno = data.frame(grp=c("A","A","A","B","B"), size=1:5, row.names=colnames(data))
col_anno
      grp size
Grp_1   A    1
Grp_2   A    2
Grp_3   A    3
Grp_4   B    4
Grp_5   B    5
pheatmap::pheatmap(data, scale="row", 
cluster_rows=FALSE, 
annotation_col=col_anno,
annotation_row=row_anno,
filename="pheatmap_1.pdf")

自定义下颜色吧。

# <bias> values larger than 1 will give more color for high end. 
# Values between 0-1 will give more color for low end.
pheatmap::pheatmap(data, scale="row", 
cluster_rows=FALSE, 
annotation_col=col_anno,
annotation_row=row_anno,
color=colorRampPalette(c('green','yellow','red'), bias=1)(50),
filename="pheatmap_1.pdf")

heatmap.2的使用就不介绍了,跟pheatmap有些类似,而且也有不少教程。

不改脚本的热图绘制

绘图时通常会碰到两个头疼的问题:

  1. 需要画很多的图,唯一的不同就是输出文件,其它都不需要修改。如果用R脚本,需要反复替换文件名,繁琐又容易出错。
  2. 每次绘图都需要不断的调整参数,时间久了不用,就忘记参数放哪了;或者调整次数过多,有了很多版本,最后不知道用哪个了。

为了简化绘图、维持脚本的一致,我用bashR做了一个封装,然后就可以通过修改命令好参数绘制不同的图了。

先看一看怎么使用

首先把测试数据存储到文件中方便调用。数据矩阵存储在heatmap_data.xls文件中;行注释存储在heatmap_row_anno.xls文件中;列注释存储在heatmap_col_anno.xls文件中。

# tab键分割,每列不加引号
write.table(data, file="heatmap_data.xls", sep="\t", row.names=T, col.names=T,quote=F)
# 如果看着第一行少了ID列不爽,可以填补下
system("sed -i '1 s/^/ID\t/' heatmap_data.xls")
write.table(row_anno, file="heatmap_row_anno.xls", sep="\t", row.names=T, col.names=T,quote=F)
write.table(col_anno, file="heatmap_col_anno.xls", sep="\t", row.names=T, col.names=T,quote=F)

然后用程序sp_pheatmap.sh绘图。

# -f: 指定输入的矩阵文件
# -d:指定是否计算Z-score,<none> (否), <row> (按行算), <col> (按列算)
# -P: 行注释文件
# -Q: 列注释文件
ct@ehbio:~/$ sp_pheatmap.sh -f heatmap_data.xls -d row -P heatmap_row_anno.xls -Q heatmap_col_anno.xls

一个回车就得到了下面的图

字有点小,是因为图太大了,把图的宽和高缩小下试试。

# -f: 指定输入的矩阵文件
# -d:指定是否计算Z-score,<none> (否), <row> (按行算), <col> (按列算)
# -P: 行注释文件
# -Q: 列注释文件
# -u: 设置宽度,单位是inch
# -v: 设置高度,单位是inch
ct@ehbio:~/$ sp_pheatmap.sh -f heatmap_data.xls -d row -P heatmap_row_anno.xls -Q heatmap_col_anno.xls -u 8 -v 12

横轴的标记水平放置

# -A: 0, X轴标签选择0度
# -C: 自定义颜色,注意引号的使用,最外层引号与内层引号不同,引号之间无交叉
# -T: 指定给定的颜色的类型;如果给的是vector (如下面的例子), 则-T需要指定为vector; 否则结果会很怪异,只有俩颜色。
# -t: 指定图形的题目,注意引号的使用;参数中包含空格或特殊字符等都要用引号引起来作为一个整体。
ct@ehbio:~/$ sp_pheatmap.sh -f heatmap_data.xls -d row -P heatmap_row_anno.xls -Q heatmap_col_anno.xls -u 8 -v 12 -A 0 -C 'c("white", "blue")' -T vector -t "Heatmap of gene expression profile"

sp_pheatmap.sh的参数还有一些,可以完成前面讲述过的所有热图的绘制,具体如下:

***CREATED BY Chen Tong (chentong_biology@163.com)***
----Matrix file--------------
Name    T0_1    T0_2    T0_3    T4_1    T4_2
TR19267|c0_g1|CYP703A2    1.431    0.77    1.309    1.247    0.485
TR19612|c1_g3|CYP707A1    0.72    0.161    0.301    2.457    2.794
TR60337|c4_g9|CYP707A1    0.056    0.09    0.038    7.643    15.379
TR19612|c0_g1|CYP707A3    2.011    0.689    1.29    0    0
TR35761|c0_g1|CYP707A4    1.946    1.575    1.892    1.019    0.999
TR58054|c0_g2|CYP707A4    12.338    10.016    9.387    0.782    0.563
TR14082|c7_g4|CYP707A4    10.505    8.709    7.212    4.395    6.103
TR60509|c0_g1|CYP707A7    3.527    3.348    2.128    3.257    2.338
TR26914|c0_g1|CYP710A1    1.899    1.54    0.998    0.255    0.427
----Matrix file--------------
----Row annorarion file --------------
------1. At least two columns--------------
------2. The first column should be the same as the first column in
         matrix (order does not matter)--------------
Name    Clan    Family
TR19267|c0_g1|CYP703A2    CYP71    CYP703
TR19612|c1_g3|CYP707A1    CYP85    CYP707
TR60337|c4_g9|CYP707A1    CYP85    CYP707
TR19612|c0_g1|CYP707A3    CYP85    CYP707
TR35761|c0_g1|CYP707A4    CYP85    CYP707
TR58054|c0_g2|CYP707A4    CYP85    CYP707
TR14082|c7_g4|CYP707A4    CYP85    CYP707
TR60509|c0_g1|CYP707A7    CYP85    CYP707
TR26914|c0_g1|CYP710A1    CYP710    CYP710
----Row annorarion file --------------
----Column annorarion file --------------
------1. At least two columns--------------
------2. The first column should be the same as the first row in
---------matrix (order does not matter)--------------
Name    Sample
T0_1    T0
T0_2    T0
T0_3    T0
T4_1    T4
T4_2    T4
----Column annorarion file --------------
Usage:
sp_pheatmap.sh options
Function:
This script is used to do heatmap using package pheatmap.
The parameters for logical variable are either TRUE or FALSE.
OPTIONS:
    -f    Data file (with header line, the first column is the
         rowname, tab seperated. Colnames must be unique unless you
        know what you are doing.)[NECESSARY]
    -t    Title of picture[Default empty title]
        ["Heatmap of gene expression profile"]
    -a    Display xtics. [Default TRUE]
    -A    Rotation angle for x-axis value (anti clockwise)
        [Default 90]
    -b    Display ytics. [Default TRUE]
    -H    Hieratical cluster for columns.
        Default FALSE, accept TRUE
    -R    Hieratical cluster for rows.
        Default TRUE, accept FALSE
    -c    Clustering method, Default "complete". 
        Accept "ward.D", "ward.D2","single", "average" (=UPGMA), 
        "mcquitty" (=WPGMA), "median" (=WPGMC) or "centroid" (=UPGMC)
    -C    Color vector. 
        Default pheatmap_default. 
        Aceept a vector containing multiple colors such as 
        <'c("white", "blue")'> will be transferred 
        to <colorRampPalette(c("white", "blue"), bias=1)(30)>
        or an R function 
        <colorRampPalette(rev(brewer.pal(n=7, name="RdYlBu")))(100)>
        generating a list of colors.
    -T    Color type, a vetcor which will be transferred as described in <-C> [vector] or
           a raw vector [direct vector] or    a function [function (default)].
    -B    A positive number. Default 1. Values larger than 1 will give more color
           for high end. Values between 0-1 will give more color for low end.    
    -D    Clustering distance method for rows.
        Default 'correlation', accept 'euclidean', 
        "manhattan", "maximum", "canberra", "binary", "minkowski". 
    -I    Clustering distance method for cols.
        Default 'correlation', accept 'euclidean', 
        "manhattan", "maximum", "canberra", "binary", "minkowski". 
    -L    First get log-value, then do other analysis.
        Accept an R function log2 or log10. 
        [Default FALSE]
    -d    Scale the data or not for clustering and visualization.
        [Default 'none' means no scale, accept 'row', 'column' to 
        scale by row or column.]
    -m    The maximum value you want to keep, any number larger willl
        be taken as this given maximum value.
        [Default Inf, Optional] 
    -s    The smallest value you want to keep, any number smaller will
        be taken as this given minimum value.
        [Default -Inf, Optional]  
    -k    Aggregate the rows using kmeans clustering. 
        This is advisable if number of rows is so big that R cannot 
        handle their hierarchical clustering anymore, roughly more than 1000.
        Instead of showing all the rows separately one can cluster the
        rows in advance and show only the cluster centers. The number
        of clusters can be tuned here.
        [Default 'NA' which means no
        cluster, other positive interger is accepted for executing
        kmeans cluster, also the parameter represents the number of
        expected clusters.]
    -P    A file to specify row-annotation with format described above.
        [Default NA]
    -Q    A file to specify col-annotation with format described above.
        [Default NA]
    -u    The width of output picture.[Default 20]
    -v    The height of output picture.[Default 20] 
    -E    The type of output figures.[Default pdf, accept
        eps/ps, tex (pictex), png, jpeg, tiff, bmp, svg and wmf)]
    -r    The resolution of output picture.[Default 300 ppi]
    -F    Font size [Default 14]
    -p    Preprocess data matrix to avoid 'STDERR 0 in cor(t(mat))'.
        Lowercase <p>.
        [Default TRUE]
    -e    Execute script (Default) or just output the script.
        [Default TRUE]
    -i    Install the required packages. Normmaly should be TRUE if this is 
        your first time run s-plot.[Default FALSE]

sp_pheatmap.sh是我写作的绘图工具s-plot的一个功能,s-plot可以绘制的图的类型还有一些,列举如下;在后面的教程中,会一一提起。

Usage:
s-plot options
Function:
This software is designed to simply the process of plotting and help
researchers focus more on data rather than technology.
Currently, the following types of plot are supported.
#### Bars
s-plot barPlot
s-plot horizontalBar
s-plot multiBar
s-plot colorBar
#### Lines
s-plot lines
#### Dots
s-plot pca
s-plot scatterplot
s-plot scatterplot3d
s-plot scatterplot2
s-plot scatterplotColor
s-plot scatterplotContour
s-plot scatterplotLotsData
s-plot scatterplotMatrix
s-plot scatterplotDoubleVariable
s-plot contourPlot
s-plot density2d
#### Distribution
s-plot areaplot
s-plot boxplot
s-plot densityPlot
s-plot densityHistPlot
s-plot histogram
#### Cluster
s-plot hcluster_gg (latest)
s-plot hcluster
s-plot hclust (depleted)
#### Heatmap
s-plot heatmapS
s-plot heatmapM
s-plot heatmap.2
s-plot pheatmap
s-plot pretteyHeatmap # obseleted
s-plot prettyHeatmap
#### Others
s-plot volcano
s-plot vennDiagram
s-plot upsetView

相关推荐

方差分析简介(方差分析通俗理解)

介绍方差分析(ANOVA,AnalysisofVariance)是一种广泛使用的统计方法,用于比较两个或多个组之间的均值。单因素方差分析是方差分析的一种变体,旨在检测三个或更多分类组的均值是否存在...

正如404页面所预示,猴子正成为断网元凶--吧嗒吧嗒真好吃

吧嗒吧嗒,绘图:MakiNaro你可以通过加热、冰冻、水淹、模塑、甚至压溃压力来使网络光缆硬化。但用猴子显然是不行的。光缆那新挤压成型的塑料外皮太尼玛诱人了,无法阻挡一场试吃盛宴的举行。印度政府正...

Python数据可视化:箱线图多种库画法

概念箱线图通过数据的四分位数来展示数据的分布情况。例如:数据的中心位置,数据间的离散程度,是否有异常值等。把数据从小到大进行排列并等分成四份,第一分位数(Q1),第二分位数(Q2)和第三分位数(Q3)...

多组独立(完全随机设计)样本秩和检验的SPSS操作教程及结果解读

作者/风仕在上一期,我们已经讲完了两组独立样本秩和检验的SPSS操作教程及结果解读,这期开始讲多组独立样本秩和检验,我们主要从多组独立样本秩和检验介绍、两组独立样本秩和检验使用条件及案例的SPSS操作...

方差分析 in R语言 and Excel(方差分析r语言例题)

今天来写一篇实际中比较实用的分析方法,方差分析。通过方差分析,我们可以确定组别之间的差异是否超出了由于随机因素引起的差异范围。方差分析分为单因素方差分析和多因素方差分析,这一篇先介绍一下单因素方差分析...

可视化:前端数据可视化插件大盘点 图表/图谱/地图/关系图

前端数据可视化插件大盘点图表/图谱/地图/关系图全有在大数据时代,很多时候我们需要在网页中显示数据统计报表,从而能很直观地了解数据的走向,开发人员很多时候需要使用图表来表现一些数据。随着Web技术的...

matplotlib 必知的 15 个图(matplotlib各种图)

施工专题,我已完成20篇,施工系列几乎覆盖Python完整技术栈,目标只总结实践中最实用的东西,直击问题本质,快速帮助读者们入门和进阶:1我的施工计划2数字专题3字符串专题4列表专题5流程控制专题6编...

R ggplot2常用图表绘制指南(ggplot2绘制折线图)

ggplot2是R语言中强大的数据可视化包,基于“图形语法”(GrammarofGraphics),通过分层方式构建图表。以下是常用图表命令的详细指南,涵盖基本语法、常见图表类型及示例,适合...

Python数据可视化:从Pandas基础到Seaborn高级应用

数据可视化是数据分析中不可或缺的一环,它能帮助我们直观理解数据模式和趋势。本文将全面介绍Python中最常用的三种可视化方法。Pandas内置绘图功能Pandas基于Matplotlib提供了简洁的绘...

Python 数据可视化常用命令备忘录

本文提供了一个全面的Python数据可视化备忘单,适用于探索性数据分析(EDA)。该备忘单涵盖了单变量分析、双变量分析、多变量分析、时间序列分析、文本数据分析、可视化定制以及保存与显示等内容。所...

统计图的种类(统计图的种类及特点图片)

统计图是利用几何图形或具体事物的形象和地图等形式来表现社会经济现象数量特征和数量关系的图形。以下是几种常见的统计图类型及其适用场景:1.条形图(BarChart)条形图是用矩形条的高度或长度来表示...

实测,大模型谁更懂数据可视化?(数据可视化和可视化分析的主要模型)

大家好,我是Ai学习的老章看论文时,经常看到漂亮的图表,很多不知道是用什么工具绘制的,或者很想复刻类似图表。实测,大模型LaTeX公式识别,出乎预料前文,我用Kimi、Qwen-3-235B...

通过AI提示词让Deepseek快速生成各种类型的图表制作

在数据分析和可视化领域,图表是传达信息的重要工具。然而,传统图表制作往往需要专业的软件和一定的技术知识。本文将介绍如何通过AI提示词,利用Deepseek快速生成各种类型的图表,包括柱状图、折线图、饼...

数据可视化:解析箱线图(box plot)

箱线图/盒须图(boxplot)是数据分布的图形表示,由五个摘要组成:最小值、第一四分位数(25th百分位数)、中位数、第三四分位数(75th百分位数)和最大值。箱子代表四分位距(IQR)。IQR是...

[seaborn] seaborn学习笔记1-箱形图Boxplot

1箱形图Boxplot(代码下载)Boxplot可能是最常见的图形类型之一。它能够很好表示数据中的分布规律。箱型图方框的末尾显示了上下四分位数。极线显示最高和最低值,不包括异常值。seaborn中...