R统计绘图 - 热图简化

欢迎关注天下博客:http://blog.genesino.com/2017/06/heatmap-simple/
Jump to...

  1. 热图绘制 - pheatmap
    1. 不改脚本的热图绘制
    2. 生信宝典,一起换个角度学生信

热图绘制 - 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")

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

image

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

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

image

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

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

image

给矩阵 (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")

image.png

自定义下颜色吧。

# <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")

image

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

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

image

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

# -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

image

横轴的标记水平放置

# -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" 

image

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

为了推广,也为了激起大家的热情,如果想要sp_pheatmap.sh脚本的,还需要劳烦大家动动手,转发此文章到朋友圈,并留言索取。

生信宝典,一起换个角度学生信

<footer class="entry-meta" style="box-sizing: border-box; display: block; font-size: 0.75rem; text-transform: uppercase; color: rgba(187, 187, 187, 0.8); margin: 50px 30px 30px; text-align: center; font-family: Lato, Calibri, Arial, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">RBIOINFOCHENTONG
版权声明:本文为博主原创文章,转载请注明出处。

alipay.png
WeChatPay.png

</footer>

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

推荐阅读更多精彩内容

  • TaoYan 简介 本文将绘制静态与交互式热图,需要使用到以下R包和函数:heatmap():用于绘制简单热图的函...
    taoyan阅读 47,031评论 4 129
  • 欢迎关注生信宝典,获取更好阅读体验。 http://mp.weixin.qq.com/s?__biz=MzI5MT...
    生信宝典阅读 4,529评论 0 15
  • 箱线图 箱线图是能同时反映数据统计量和整体分布,又很漂亮的展示图。在2014年的Nature Method上有2篇...
    生信宝典阅读 22,358评论 1 22
  • 线图 线图是反映趋势变化的一种方式,其输入数据一般也是一个矩阵。 单线图 假设有这么一个矩阵,第一列为转录起始位点...
    生信宝典阅读 883评论 0 0
  • 这个暑期档 各大平台网剧的收视率争夺战 打得异常火热 比起赚足话题的《鬼吹灯》系列 《河神》依靠神秘诡谲的画风 和...
    剧透社阅读 381评论 1 3