R语言学习 - 热图简化
前面推出過熱圖繪制和熱圖美化,現在來一個函數繪制熱圖的簡化方式。文后更有不用寫代碼的在線工具可用。
R語言 - 基礎概念和矩陣操作
R語言 - 熱圖簡化
R語言 - 熱圖繪制 (heatmap)
R語言 - 熱圖美化
熱圖繪制 - 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.0pheatmap::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 ?clu2col_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 ? ?5pheatmap::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有些類似,而且也有不少教程。
不改腳本的熱圖繪制
繪圖時通常會碰到兩個頭疼的問題:
需要畫很多的圖,唯一的不同就是輸出文件,其它都不需要修改。如果用R腳本,需要反復替換文件名,繁瑣又容易出錯。
每次繪圖都需要不斷的調整參數,時間久了不用,就忘記參數放哪了;或者調整次數過多,有了很多版本,最后不知道用哪個了。
為了簡化繪圖、維持腳本的一致,我用bash對R做了一個封裝,然后就可以通過修改命令好參數繪制不同的圖了。
先看一看怎么使用
首先把測試數據存儲到文件中方便調用。數據矩陣存儲在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 inmatrix (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 optionsFunction: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 therowname, tab seperated. Colnames must be unique unless youknow 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] ora raw vector [direct vector] or ? ?a function [function (default)].-B ? ?A positive number. Default 1. Values larger than 1 will give more colorfor 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 willlbe taken as this given maximum value.[Default Inf, Optional] -s ? ?The smallest value you want to keep, any number smaller willbe 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 therows in advance and show only the cluster centers. The numberof clusters can be tuned here.[Default 'NA' which means nocluster, other positive interger is accepted for executingkmeans cluster, also the parameter represents the number ofexpected 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, accepteps/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 optionsFunction: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腳本的,還需要勞煩大家動動手,轉發此文章到朋友圈,并后臺回復 s-plot 索取。
易生信系列培訓課程,掃碼獲取免費資料
更多閱讀
畫圖三字經?生信視頻?生信系列教程?
心得體會?TCGA數據庫?Linux?Python?
高通量分析?免費在線畫圖?測序歷史?超級增強子
生信學習視頻?PPT?EXCEL?文章寫作?ggplot2
海哥組學?可視化套路?基因組瀏覽器
色彩搭配?圖形排版?互作網絡
自學生信
后臺回復“生信寶典福利第一波”獲取教程合集
高顏值免費在線繪圖
R統計和作圖
Graphpad,經典繪圖工具初學初探
在R中贊揚下努力工作的你,獎勵一份CheatShet
別人的電子書,你的電子書,都在bookdown
R語言 - 入門環境Rstudio
R語言 - 熱圖繪制 (heatmap)
R語言 - 基礎概念和矩陣操作
R語言 - 熱圖簡化
R語言 - 熱圖美化
R語言 - 線圖繪制
R語言 - 線圖一步法
R語言 - 箱線圖(小提琴圖、抖動圖、區域散點圖)
R語言 - 箱線圖一步法
R語言 - 火山圖
R語言 - 富集分析泡泡圖
R語言 - 散點圖繪制
R語言 - 韋恩圖
R語言 - 柱狀圖
R語言 - 圖形設置中英字體
R語言 - 非參數法生存分析
R語言 - 繪制seq logo圖
WGCNA分析,簡單全面的最新教程
psych +igraph:共表達網絡構建
一文學會網絡分析——Co-occurrence網絡圖在R中的實現
一文看懂PCA主成分分析
富集分析DotPlot,可以服
基因共表達聚類分析和可視化
R中1010個熱圖繪制方法
還在用PCA降維?快學學大牛最愛的t-SNE算法吧, 附Python/R代碼
一個函數抓取代謝組學權威數據庫HMDB的所有表格數據
文章用圖的修改和排版
network3D: 交互式桑基圖
network3D 交互式網絡生成
Seq logo 在線繪制工具——Weblogo
生物AI插圖素材獲取和拼裝指導
ggplot2高效實用指南 (可視化腳本、工具、套路、配色)
圖像處理R包magick學習筆記
SOM基因表達聚類分析初探
利用gganimate可視化全球范圍R-Ladies(R社區性別多樣性組織)發展情況
一分鐘繪制磷脂雙分子層:AI零基礎入門和基本圖形繪制
AI科研繪圖(二):模式圖的基本畫法
你知道R中的賦值符號箭頭(<-)和等號(=)的區別嗎?
R語言可視化學習筆記之ggridges包
利用ComplexHeatmap繪制熱圖(一)
ggplot2學習筆記之圖形排列
R包reshape2,輕松實現長、寬數據表格轉換
用R在地圖上繪制網絡圖的三種方法
PCA主成分分析實戰和可視化 附R代碼和測試數據
iTOL快速繪制顏值最高的進化樹!
12個ggplot2擴展包幫你實現更強大的可視化
編程模板-R語言腳本寫作:最簡單的統計與繪圖,包安裝、命令行參數解析、文件讀取、表格和矢量圖輸出
R語言統計入門課程推薦——生物科學中的數據分析Data Analysis for the Life Sciences
數據可視化基本套路總結
你知道R中的賦值符號箭頭<-和等號=的區別嗎?
使用dplyr進行數據操作30例
交集intersect、并集union、找不同setdiff
R包reshape2,輕松實現長、寬數據表格轉換
1數據類型(向量、數組、矩陣、 列表和數據框)
2讀寫數據所需的主要函數、與外部環境交互
3數據篩選——提取對象的子集
4向量、矩陣的數學運算
5控制結構
6函數及作用域
7認識循環函數lapply和sapply
8分解數據框split和查看對象str
9模擬—隨機數、抽樣、線性模型
1初識ggplot2繪制幾何對象
2圖層的使用—基礎、加標簽、注釋
3工具箱—誤差線、加權數、展示數據分布
4語法基礎
5通過圖層構建圖像
6標度、軸和圖例
7定位-分面和坐標系
8主題設置、存儲導出
9繪圖需要的數據整理技術
創建屬于自己的調色板
28個實用繪圖包,總有幾個適合你
熱圖繪制
R做線性回歸
繪圖相關系數矩陣corrplot
相關矩陣可視化ggcorrplot
繪制交互式圖形recharts
交互式可視化CanvasXpress
聚類分析factoextra
LDA分析、作圖及添加置信-ggord
解決散點圖樣品標簽重疊ggrepel
添加P值或顯著性標記ggpubr
Alpha多樣性稀釋曲線rarefraction curve
堆疊柱狀圖各成分連線畫法:突出組間變化
沖擊圖展示組間時間序列變化ggalluvial
桑基圖riverplot
微生物環境因子分析ggvegan
五彩進化樹與熱圖更配ggtree
多元回歸樹分析mvpart
隨機森林randomForest?分類Classification?回歸Regression
加權基因共表達網絡分析WGCNA
circlize包繪制circos-plot
R語言搭建炫酷的線上博客系統
28個實用繪圖包,總有幾個適合你
Cytoscape教程1
Cytoscape之操作界面介紹
新出爐的Cytoscape視頻教程
Cytoscape制作帶bar圖和pie圖節點的網絡圖
Cytoscape: MCODE增強包的網絡模塊化分析
聽說分享到朋友圈的朋友會在公眾號周年慶時中獎 (大家還記得去年的大放送吧,不記得查查歷史)
學習宏基因組關照此公眾號。
總結
以上是生活随笔為你收集整理的R语言学习 - 热图简化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hemberg-lab单细胞转录组数据分
- 下一篇: Conda solving enviro