R绘图|pheatmap热图绘制——中阶篇

本次内容在pheatmap热图绘制——基础篇的基础上展示如何在热图上体现不同的功能分区,以及如何根据聚类结果将不同的cluster分开?

首先清除环境,安装并加载所需要的R包

rm(list = ls()) #清除环境内存
#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) #用于输出不同格式的结果

读入分组信息,用于行注释

rm(list = ls()) #清除环境内存
#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) #用于输出不同格式的结果

读入表达数据并对数据做简单处理

data<-read_excel("demo2.xlsx",1)    #读入excel数据
data<-as.data.frame(data) # 将data转换为data.frame格式
rownames(data)<-data[,1]  #将第一列数据设置为行名
data<-data[,-1] #去除重复的第一列
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

构建样本信息,用于列注释

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

绘图并输出结果

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包输出结果
#graph2pdf(file="demo2.pdf",width=6,height=8)#导出pdf格式文件
#graph2tif(file="demo2.tif",width=6,height=8)#导出tiff格式文件
#graph2jpg(file="demo2.jpg",width=6,height=8)#导出jpg格式文件

添加gap,区分不同cluster

1. 可以用cutree或者gap参数去定义,cutree只有在聚类的基础上方可使用,取值可聚类树大致的分块。

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可自定义间隔的位置,适用于那种不聚类保证美观顺序的情况,肉眼可区分分段区域

table(bx)#查看各个功能的基因数量
## 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)

显示运行环境

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散点图的绘制
R绘图|pheatmap热图绘制——基础篇

今天的内容就到这里~~,更多内容可关注公共号“YJY技能修炼”~~

你可能感兴趣的:(R绘图|pheatmap热图绘制——中阶篇)