前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言复杂热图的绘制

R语言复杂热图的绘制

作者头像
一粒沙
发布2019-07-31 11:16:34
2.8K0
发布2019-07-31 11:16:34
举报

大家对热图应该都不陌生,但是混合的复杂热图在我们的应用中并不是太多见。今天给大家介绍一个绘制复杂热图的R包ComplexHeatmap。

首先我们看下安装和载入,其安装通过bioconductor安装:

source("https://bioconductor.org/biocLite.R")

biocLite("ComplexHeatmap")

安装后载入成功如下图:

我们看下包的主要功能,其主要通过以下布局将箱线图、散点图等整合到一起。

这是其中主要的两个进行布局的功能类:

HeatmapAnnotation()主要是构建绘图的annotation部分。

实例代码:

value = rnorm(10)

ha = HeatmapAnnotation(barplot1 =anno_barplot(value, baseline = 0, gp = gpar(fill = ifelse(value > 0, "red","green"))),

points =anno_points(value, gp = gpar(col = rep(1:2, 5))),

barplot2 =anno_barplot(value, gp = gpar(fill = rep(3:4, 5))))

Heatmap()主要构建布局,其中主要用到的参数:

top_annotation =new("HeatmapAnnotation")顶部绘制的图像

top_annotation_height = top_annotation@size顶部高度这里的高度主要的值是eg:unit(8, "cm")

bottom_annotation =new("HeatmapAnnotation")底部绘制的图像

bottom_annotation_height =bottom_annotation@size底部的高度

实例代码:

#下面是正常的图形布局绘制

mat = matrix(rnorm(80, 2), 8, 10)

mat = rbind(mat, matrix(rnorm(40, -2), 4,10))

rownames(mat) = paste0("R", 1:12)

colnames(mat) = paste0("C", 1:10)

Heatmap(mat, top_annotation = ha,bottom_annotation = ha)

#下面是中间的热图提供数据,此处直接可以不绘制热图只绘制我们想要结合在一起的图。

zero_row_mat = matrix(nrow = 0, ncol = 10)

colnames(zero_row_mat) = letters[1:10]

Heatmap(zero_row_mat, top_annotation = ha,column_title = "only annotations")

其中主要的函数是:

oncoPrint()其为绘制热图的核心函数,其主要可以对热图的中的cell进行分割,更加细致显示数据的分布。其主要参数如下:

其中主要应有的参数如下:

top_annotation = HeatmapAnnotation(points =anno_points(value, gp = gpar(col = rep(1:2, 5))),annotation_height = unit(8,"cm"))决定顶部绘图部分的图形大小以及对应的内容。

annotation_height = unit(8,"cm"))决定顶部图形高度

show_pct=FALSE 是否显示得到的百分比

row_names_gp = gpar(fontsize = 8)设置核心部分行名称的大小

实例如下:

library(ComplexHeatmap)

#这部分主要是数据的读入,其主要形式可以打开看下

mat =read.table(paste0(system.file("extdata", package ="ComplexHeatmap"),

"/tcga_lung_adenocarcinoma_provisional_ras_raf_mek_jnk_signalling.txt"),

header = TRUE,stringsAsFactors=FALSE, sep = "\t")

mat[is.na(mat)] = ""

rownames(mat) = mat[, 1]

mat = mat[, -1]

mat= mat[, -ncol(mat)]

mat = t(as.matrix(mat))

#下面部分主要是要和数据中所包含的类型对应,并且数量一致。

alter_fun = list(

background = function(x, y, w, h) {

grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5,"mm"), gp = gpar(fill = "#CCCCCC", col = NA))

},

HOMDEL = function(x, y, w, h) {

grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.5,"mm"), gp = gpar(fill = "blue", col = NA))

},

AMP = function(x, y, w, h) {

grid.rect(x, y, w-unit(0.5,"mm"), h-unit(0.5, "mm"), gp = gpar(fill = "red",col = NA))

},

MUT = function(x, y, w, h) {

grid.rect(x, y, w-unit(0.5, "mm"), h*0.33, gp = gpar(fill ="#008000", col = NA))

}

)

#颜色的设置与类型一致

col = c("MUT" ="#008000", "AMP" = "red", "HOMDEL" ="blue")

#下面是主函数,主要是绘制图像

oncoPrint(mat, get_type = function(x)strsplit(x, ";")[[1]],top_annotation = HeatmapAnnotation(points =anno_points(value, gp = gpar(col = rep(1:2, 5))),

annotation_height = unit(8, "cm")), row_title_side = "left",

top_annotation_height = unit(8, "cm"),show_pct=FALSE,

alter_fun = alter_fun, col = col, show_row_barplot =FALSE,row_names_gp =gpar(fontsize = 8),

column_title = "OncoPrint for TCGA Lung Adenocarcinoma, genes inRas Raf MEK JNK signalling",

heatmap_legend_param = list(title = "Alternations", at =c("AMP", "HOMDEL", "MUT"),

labels = c("Amplification", "Deep deletion","Mutation")))

draw() 主要是对HeatmapAnnotation()形成的项目进行图像的绘制,一般主要是颜色bar的形成靠这个函数。并且图像可以叠加。

这个包还提供了一个好玩的功能那就是图形的交互函数

selectArea(mark = TRUE)#运行后,鼠标变成十字架,第一次点击是左上角,第二次点击是右下角。获取矩形的数据。

运行这个函数可以允许我们在绘制的图形中进行选择对应的区域以及此区域包含的值。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-09-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言交流中心 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档