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技能修炼”~~

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