R-作图

R绘图|pheatmap热图绘制——基础篇

2020-06-18  本文已影响0人  Clariom

清除环境及安装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) #用于输出不同格式的结果

读入数据并做简单处理

data<-read_excel("demo1.xlsx",1)    #读入excel数据
data<-as.data.frame(data) # 将data转换为data.frame格式
rownames(data)<-data[,1]  #将第一列数据设置为行名
data<-data[,-1] #去除重复的第一列
colnames(data) = gsub("_expression","",colnames(data))
head(data)
##          CKD1  CKD2  CKD3  CKD4  CKD5 Ctrl1 Ctrl2 Ctrl3 Ctrl4 Ctrl5\r\n
## Fam117b 10.56 10.26 10.99 11.17 11.02  8.28  9.10  9.20  9.34      8.76
## Fkbp5   10.99 10.48 10.89 11.75 10.80  9.44  7.64  8.82  7.92      8.17
## Bcl2l1   9.73  9.50 10.20  9.44  9.98  8.04  8.42  8.77  8.14      7.94
## Zbtb16  12.33 12.36 12.69 11.59 13.21  9.58 10.66 10.11 10.75      9.05
## Fam107a 10.00  9.12  9.21  9.11  8.99  6.99  7.37  6.71  8.15      6.11
## Adrm1    7.50  7.46  7.42  7.80  7.55  6.69  6.69  6.57  6.53      6.83

绘图并输出结果

p=pheatmap(data,fontsize_col=9,fontsize_row=8,
         show_rownames=F,show_colnames = T,
         cluster_row=T,cluster_col=T,scale="row",
         treeheight_row=25,treeheight_col=25,
         border_color=NA,main="Heatmap")  # 画图
print(p)
image
ggsave(p,filename = "pheatmap.pdf",width=6,height=8)
ggsave(p,filename = "pheatmap.png",width=6,height=8)
#也可以加载export包输出结果
#graph2pdf(file="pheatmap.pdf",width=6,height=8)#导出pdf格式文件
#graph2tif(file="pheatmap.tif",width=6,height=8)#导出tiff格式文件
#graph2jpg(file="pheatmap.jpg",width=6,height=8)#导出jpg格式文件

颜色设置

修改热图颜色1

p1=pheatmap(data,fontsize_col=9,fontsize_row=8,
         show_rownames=F,show_colnames = T,
         cluster_row=T,cluster_col=T,scale="row",
         treeheight_row=25,treeheight_col=25,
         border_color=NA,main="Heatmap",
         color = colorRampPalette(c("green", "black", "red"))(50))  # 画图
print(p1)
image
ggsave(p,filename = "demo1_1.pdf",width=6,height=8)
ggsave(p,filename = "demo1_1.png",width=6,height=8)

修改热图颜色2

p2=pheatmap(data,fontsize_col=9,fontsize_row=8,
         show_rownames=F,show_colnames = T,
         cluster_row=T,cluster_col=T,scale="row",
         treeheight_row=25,treeheight_col=25,
         border_color=NA,main="Heatmap",
         color = colorRampPalette(c("blue", "white", "red"))(50))  # 画图
print(p2)      
image
ggsave(p2,filename = "demo1_2.pdf",width=6,height=8)
ggsave(p2,filename = "demo1_2.png",width=6,height=8)

修改热图颜色3

p3=pheatmap(data,fontsize_col=9,fontsize_row=8,
         show_rownames=F,show_colnames = T,
         cluster_row=T,cluster_col=T,scale="row",
         treeheight_row=25,treeheight_col=25,
         border_color=NA,main="Heatmap",
         color = colorRampPalette(c("blue", "white", "yellow"))(50))  # 画图
print(p3)      
image.png
ggsave(p3,filename = "demo1_3.pdf",width=6,height=8)
ggsave(p3,filename = "demo1_3.png",width=6,height=8)

显示基因名称

以上数据非常多,选择部分数据绘图

library(pheatmap) #加载pheatmap包
library(readxl)  #加载readxl包
data<-read_excel("demo1.xlsx",1)    #读入excel数据
data<-as.data.frame(data) # 将data转换为data.frame格式
rownames(data)<-data[,1]  #将第一列数据设置为行名
data<-data[,-1] #去除重复的第一列
data_part<-head(data,15) #取前15行进一步绘图
colnames(data_part) = gsub("_expression","",colnames(data_part))# 设置列名
p4=pheatmap(data_part,
         fontsize_col=9,fontsize_row=8,
         show_rownames=T,show_colnames = T,
         cluster_row=T,cluster_col=T,scale="row",
         treeheight_row=25,treeheight_col=25,
         border_color=NA,main="Heatmap",
         color = colorRampPalette(c("blue", "white", "yellow"))(50))  # 画图
print(p4)        
image
ggsave(p4,filename = "demo1_4.pdf",width=6,height=8)
ggsave(p4,filename = "demo1_4.png",width=6,height=8)

调整格子大小并显示数值

p5=pheatmap(data_part,fontsize_col=9,fontsize_row=8,
         show_rownames=T,show_colnames = T,
         cluster_row=T,cluster_col=T,scale="row",
         treeheight_row=25,treeheight_col=25,
         border_color=NA,main="Heatmap",
         color = colorRampPalette(c("blue", "white", "yellow"))(50),
         display_numbers = TRUE,
         cellwidth = 30, cellheight = 30)  # 画图
print(p5)   
image
ggsave(p5,filename = "demo1_5.pdf",width=6,height=8)
ggsave(p5,filename = "demo1_5.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           promises_1.1.0
## [52] xml2_1.2.2              vctrs_0.2.3             generics_0.0.2
## [55] openxlsx_4.1.4          stargazer_5.2.2         RColorBrewer_1.1-2
## [58] tools_3.6.2             manipulateWidget_0.10.1 glue_1.3.1
## [61] officer_0.3.8           purrr_0.3.3             crosstalk_1.0.0
## [64] fastmap_1.0.1           yaml_2.2.1              colorspace_1.4-1
## [67] knitr_1.28

往期回顾
R绘图|ggplot2散点图的绘制

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

上一篇下一篇

猜你喜欢

热点阅读