R繪圖|pheatmap熱圖繪制——中階篇

本次內(nèi)容在pheatmap熱圖繪制——基礎(chǔ)篇的基礎(chǔ)上展示如何在熱圖上體現(xiàn)不同的功能分區(qū),以及如何根據(jù)聚類結(jié)果將不同的cluster分開(kāi)?

首先清除環(huán)境,安裝并加載所需要的R包

rm(list = ls()) #清除環(huán)境內(nèi)存
#install.packages("pheatmap")  #安裝pheatmap包
#install.packages("readxl")  #安裝readxl包
#install.packages("ggplot2") #安裝ggplot2包
#install.packages(c('officer', 'rvg', 'flextable', 'stargazer', 'broom' ))#export的依賴包
#下載export,鏈接https://cran.r-project.org/src/contrib/Archive/export/export_0.2.2.tar.gz
#install.packages("D:/R-3.6.2/library/export_0.2.2.tar.gz", repos = NULL)
library(ggplot2)
library(pheatmap) #加載pheatmap包
library(readxl)  #加載readxl包
library(export) #用于輸出不同格式的結(jié)果

讀入分組信息,用于行注釋

rm(list = ls()) #清除環(huán)境內(nèi)存
#install.packages("pheatmap")  #安裝pheatmap包
#install.packages("readxl")  #安裝readxl包
#install.packages("ggplot2") #安裝ggplot2包
#install.packages(c('officer', 'rvg', 'flextable', 'stargazer', 'broom' ))#export的依賴包
#下載export,鏈接https://cran.r-project.org/src/contrib/Archive/export/export_0.2.2.tar.gz
#install.packages("D:/R-3.6.2/library/export_0.2.2.tar.gz", repos = NULL)
library(ggplot2)
library(pheatmap) #加載pheatmap包
library(readxl)  #加載readxl包
library(export) #用于輸出不同格式的結(jié)果

讀入表達(dá)數(shù)據(jù)并對(duì)數(shù)據(jù)做簡(jiǎn)單處理

data<-read_excel("demo2.xlsx",1)    #讀入excel數(shù)據(jù)
data<-as.data.frame(data) # 將data轉(zhuǎn)換為data.frame格式
rownames(data)<-data[,1]  #將第一列數(shù)據(jù)設(shè)置為行名
data<-data[,-1] #去除重復(fù)的第一列
colnames(data) = gsub("_FPKM","",colnames(data))#去除列名中的_FPKM
head(data)
##         negative_1 negative_3 negative_4 neutral_1 neutral_3 neutral_4
## NCL       2.059622   1.896343   1.092175  2.936300  3.181986  4.710491
## PLEKHG5   0.712203   0.871737   0.493767  1.699708  0.652557  2.117785
## EIF2S3    1.590397   1.457507   1.710653  3.299838  2.262812  4.715623
## SMAD7     0.183231   0.102905   0.159121  0.298067  0.365457  0.299725
## HDLBP     0.443738   0.439725   0.297456  0.967719  0.845097  0.744160
## PTPRM     0.456168   0.330697   0.342640  0.760930  0.746550  0.957069

構(gòu)建樣本信息,用于列注釋

sp= data.frame(CellType = c(rep("negative", 3),rep("neutral", 3)),Time = colnames(data))
rownames(sp)<-sp[,2]
sp[,2]<-NULL
head(sp)
##            CellType
## negative_1 negative
## negative_3 negative
## negative_4 negative
## neutral_1   neutral
## neutral_3   neutral
## neutral_4   neutral

繪圖并輸出結(jié)果

p=pheatmap(data,fontsize_col=12,
         show_rownames=F,
         cluster_cols = T,cluster_rows = T,scale="row",
         treeheight_row=25,treeheight_col=25,
         main="Heatmap",
         color = colorRampPalette(c("blue", "white", "red"))(50),
         annotation_row=bx,annotation_col = sp,annotation_legend = T)
image
ggsave(p,filename = "demo2.pdf",width=6,height=8)
ggsave(p,filename = "demo2.png",width=6,height=8)
#也可以加載export包輸出結(jié)果
#graph2pdf(file="demo2.pdf",width=6,height=8)#導(dǎo)出pdf格式文件
#graph2tif(file="demo2.tif",width=6,height=8)#導(dǎo)出tiff格式文件
#graph2jpg(file="demo2.jpg",width=6,height=8)#導(dǎo)出jpg格式文件

添加gap,區(qū)分不同cluster

1. 可以用cutree或者gap參數(shù)去定義,cutree只有在聚類的基礎(chǔ)上方可使用,取值可聚類樹(shù)大致的分塊。

p1=pheatmap(data,fontsize_col=12,
         show_rownames=F,
         cluster_cols = T,cluster_rows = T,scale="row",
         annotation_row=bx,annotation_col = sp,annotation_legend = T,
         treeheight_row=25,treeheight_col=25,
         main="Heatmap",
         color = colorRampPalette(c("blue", "white", "red"))(50),
         cutree_row = 4,cutree_col = 2)
image
ggsave(p1,filename = "demo2_gap1.pdf",width=6,height=8)
ggsave(p1,filename = "demo2_gap1.png",width=6,height=8)

2. gap可自定義間隔的位置,適用于那種不聚類保證美觀順序的情況,肉眼可區(qū)分分段區(qū)域

table(bx)#查看各個(gè)功能的基因數(shù)量
## bx
##     Cell cycle       Adhesion       Junction      Migration Tube formation
##             40             40            126             49              3
## Vasculogenesis
##              9
p2=pheatmap(data,fontsize_col=12,
         show_rownames=F,
         cluster_cols = T,cluster_rows = F,scale="row",
         annotation_row=bx,annotation_col = sp,annotation_legend = T,
         treeheight_row=25,treeheight_col=25,
         main="Heatmap",
         color = colorRampPalette(c("blue", "white", "red"))(50),
         gaps_row=c(40,40+40,80+126,80+126+49,80+126+49+3,80+126+49+3+9),cutree_cols = 2)
image
ggsave(p2,filename = "demo2_gap2.pdf",width=6,height=8)
ggsave(p2,filename = "demo2_gap2.png",width=6,height=8)

顯示運(yùn)行環(huán)境

sessionInfo()
## R version 3.6.2 (2019-12-12)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base
##
## other attached packages:
## [1] export_0.2.2    readxl_1.3.1    pheatmap_1.0.12 ggplot2_3.2.1
##
## loaded via a namespace (and not attached):
##  [1] rgl_0.100.50            Rcpp_1.0.3              lattice_0.20-38
##  [4] tidyr_1.0.2             assertthat_0.2.1        digest_0.6.24
##  [7] mime_0.9                R6_2.4.1                cellranger_1.1.0
## [10] backports_1.1.5         evaluate_0.14           pillar_1.4.3
## [13] gdtools_0.2.1           rlang_0.4.4             lazyeval_0.2.2
## [16] uuid_0.1-4              miniUI_0.1.1.1          data.table_1.12.8
## [19] flextable_0.5.9         rmarkdown_2.1           webshot_0.5.2
## [22] stringr_1.4.0           htmlwidgets_1.5.1       munsell_0.5.0
## [25] shiny_1.4.0             broom_0.5.5             compiler_3.6.2
## [28] httpuv_1.5.2            xfun_0.12               pkgconfig_2.0.3
## [31] systemfonts_0.1.1       base64enc_0.1-3         rvg_0.2.4
## [34] htmltools_0.4.0         tidyselect_1.0.0        tibble_2.1.3
## [37] crayon_1.3.4            dplyr_0.8.4             withr_2.1.2
## [40] later_1.0.0             grid_3.6.2              nlme_3.1-142
## [43] jsonlite_1.6.1          xtable_1.8-4            gtable_0.3.0
## [46] lifecycle_0.1.0         magrittr_1.5            scales_1.1.0
## [49] zip_2.0.4               stringi_1.4.6           farver_2.0.3
## [52] promises_1.1.0          xml2_1.2.2              vctrs_0.2.3
## [55] generics_0.0.2          openxlsx_4.1.4          stargazer_5.2.2
## [58] RColorBrewer_1.1-2      tools_3.6.2             manipulateWidget_0.10.1
## [61] glue_1.3.1              officer_0.3.8           purrr_0.3.3
## [64] crosstalk_1.0.0         fastmap_1.0.1           yaml_2.2.1
## [67] colorspace_1.4-1        knitr_1.28

往期回顧
R繪圖|ggplot2散點(diǎn)圖的繪制
R繪圖|pheatmap熱圖繪制——基礎(chǔ)篇

今天的內(nèi)容就到這里~~,更多內(nèi)容可關(guān)注公共號(hào)“YJY技能修煉”~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容