之前的DiagHeatmap是用base plot来进行绘图的,但是使用complexheatmap也可以用来绘制。这次就用complexheatmap来进行绘制一下
options("repos"= c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
PS: ComplexHeatmap包的安装需要安装github版本的。很多函数在bioconductor版本没有。
library(ComplexHeatmap)
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.1.2
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
##
## If you use it in published research, please cite:
## Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional
## genomic data. Bioinformatics 2016.
## ========================================
library(circlize)
## ========================================
## circlize version 0.4.8
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: http://jokergoo.github.io/circlize_book/book/
##
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
## in R. Bioinformatics 2014.
## ========================================
Sys.setenv(LANGUAGE = "en") #显示英文报错信息
options(stringsAsFactors = FALSE) #禁止chr转成factor
输入文件使用之前的示例文件
# 左上角的数据
up <- read.table("easy_input_amp.txt",sep = "\t",row.names = 1,check.names = F,stringsAsFactors = F,header = T)
up[1:3, 1:3]
## KIRC KIRP KICH
## ALKBH5 0.04924242 0.5902778 0.00000000
## FTO 0.18560606 0.5243056 0.31818182
## ZC3H13 0.03977273 0.1006944 0.03030303
# 右下角的数据
dn <- read.table("easy_input_del.txt",sep = "\t",row.names = 1,check.names = F,stringsAsFactors = F,header = T)
dn[1:3, 1:3]
## KIRC KIRP KICH
## ALKBH5 0.08522727 0.04861111 0.75757576
## FTO 0.04166667 0.02430556 0.07575758
## ZC3H13 0.15340909 0.08333333 0.66666667
# 检验两个矩阵列名和行名是一致的
identical(rownames(up), rownames(dn))
## [1] TRUE
identical(colnames(up), colnames(dn))
## [1] TRUE
由于加载的数据的基因名和注释的结果的列是反的,重新排列数据框
RightOrder <- rev(rownames(up))
up <- up[RightOrder,]
dn <- dn[RightOrder,]
对于ComplexHeatmap当中的颜色设置都是通过,colorRamp2来进行设置的
UpColor <- colorRamp2(breaks = c(0, 1), colors = c("#FFFADD","#AB221F"))
DnColor <- colorRamp2(breaks = c(0, 1), colors = c("#FFFADD","#3878C1"))
使用ComplexHeatmap绘图的原理主要是
## 使用up数据集来产生数据
Heatmap(up,
column_title = "Copy number variation across cancer types", ## 列的标题
rect_gp = gpar(type = "none"), #绘制空的数据框
show_heatmap_legend = F, ##是否显示基本的注释说明
cluster_rows = T, cluster_columns = T, ## 是否对行列进行聚类
)
## Warning: The input is a data frame, convert it to the matrix.
使用HeatmapAnnotation绘制注释信息,
row_an <- HeatmapAnnotation(type = c(rep("R", 10), rep("W", 8), rep("E", 2)), ##注释信息的内容。
show_annotation_name = F, ## 是否显示注释的标题
col = list(type = c("R" = "#5AC9FA", "W" = "#FAC67A", "E" = "#51B743")), ## 注释信息的颜色
show_legend = T, ## 是否显示注释信息的说明
annotation_legend_param = list(title = "m6A group",nrow = 1), ## 注释信息图例的个性化说明,nrow表示把所有分类的图例放到一行。
which = "row" #对行或者列进行注释
)
cell_fun参数来自定义每个热图方格内显示的内容。这里我们主要通过两个grid.polygon绘制两个三角形,三角形当中的填充色分别是up和down的值。首先自定义一个接受两个数据集合的函数
DiagFunc <- function(up, down){
function(j, i, x, y, width, height, fill){
grid.polygon(unit.c(x - 0.5*width, x - 0.5*width, x + 0.5*width),
unit.c(y - 0.5*height, y + 0.5*height, y + 0.5*height),
gp = gpar(fill = DnColor(down[i, j]), col = "grey"))
grid.polygon(unit.c(x + 0.5*width, x + 0.5*width, x - 0.5*width),
unit.c(y + 0.5*height, y - 0.5*height, y - 0.5*height),
gp = gpar(fill = UpColor(up[i, j]), col = "grey"))
}
}
p1 <- Heatmap(up, column_title = "Copy number variation across cancer types", rect_gp = gpar(type = "none"), show_heatmap_legend = F, cluster_rows = F, cluster_columns = T, ##绘制空的热图框
left_annotation = row_an, ##添加左侧注释信息
cell_fun = DiagFunc(up = up, down = dn) ## 绘制表格内的内容
); p1
## Warning: The input is a data frame, convert it to the matrix.
图片基本绘制完成,但是热图当中的颜色还没有具体的说明,ComplexHeatmap可以通过Legend绘制自定义自定义注释。
col_fun = colorRamp2(c(-1, 0, 1), c("#3878C1", "#FFFADD", "#AB221F")) ##自定义颜色信息
lgd <- Legend(title = "CNV Frequency", ## 注释的标题
col_fun = col_fun, ## 注释的颜色
at = c(-1,-0.5,0,0.5,1), ## 注释刻度的分组
labels = c("1","Loss","0","Gain","1"), ## 注释刻度的重命名
direction = "horizontal" ## 注释的方向
)
通过draw函数可以把热图和注释融合到一起
draw(p1, annotation_legend_list = lgd, annotation_legend_side = "bottom",heatmap_legend_side = "bottom", merge_legend = TRUE)