如何在Complexheatmap绘制的Oncoprint旁添加显著性星号?
在ComplexHeatmap的OncoPrint旁添加显著性星号的解决方案
问题描述
我已使用R中的ComplexHeatmap包绘制出Oncoprint,当前代码如下:
column_title = "OncoPrint " heatmap_legend_param = list(title = "Alternations", at = c("UP", "AMP", "MUT"), labels = c("Over Expression", "Amplification", "Mutation")) oncoPrint(df1, alter_fun = alter_fun, col = col, column_title = column_title, heatmap_legend_param = heatmap_legend_param, right_annotation = NULL, top_annotation = NULL, row_names_side = "left", show_pct = FALSE)
我希望在该Oncoprint旁添加类似样式的显著性星号(如单星*、双星**、三星***标注),尝试过结合ComplexHeatmap与ggplot2但无法兼容,想了解是否有可行方法实现?
可行解决方案
ComplexHeatmap基于grid绘图系统,无需结合ggplot2,直接用内置注释功能或grid包即可实现需求,以下是两种实用方法:
方法1:利用rowAnnotation批量添加显著性星号
适合给所有基因行统一添加标记,步骤如下:
- 准备好对应每行基因的显著性星号向量(比如
sig_stars,元素为*、**、***或空字符串); - 创建行注释对象;
- 在绘制OncoPrint时将注释绑定到热图一侧。
示例代码:
# 示例:假设df1有5行基因,对应显著性星号 sig_stars <- c("*", "", "***", "**", "*") # 创建右侧行注释 sig_anno <- rowAnnotation( significance = anno_text( sig_stars, location = 0.5, # 水平居中 gp = gpar(fontsize = 12, fontface = "bold", col = "red") # 设置星号样式 ), width = unit(0.5, "cm") # 注释栏宽度 ) # 绘制带注释的OncoPrint oncoPrint(df1, alter_fun = alter_fun, col = col, column_title = column_title, heatmap_legend_param = heatmap_legend_param, right_annotation = sig_anno, # 把注释放在热图右侧 top_annotation = NULL, row_names_side = "left", show_pct = FALSE)
方法2:绘图后用grid包手动添加星号
适合给特定行添加标记,或需要灵活调整星号位置的场景:
- 先绘制OncoPrint并渲染到设备;
- 获取目标行的坐标;
- 用
grid.text逐个添加星号。
示例代码:
# 先绘制OncoPrint p <- oncoPrint(df1, alter_fun = alter_fun, col = col, column_title = column_title, heatmap_legend_param = heatmap_legend_param, right_annotation = NULL, top_annotation = NULL, row_names_side = "left", show_pct = FALSE) draw(p) # 将图渲染到当前绘图设备 # 获取每行基因的y轴坐标(基于grid的npc坐标系) n_rows <- nrow(df1) y_coords <- seq(from = 1 - 1/(2*n_rows), to = 1/(2*n_rows), length.out = n_rows) # 给第1、3、5行添加星号 target_rows <- c(1, 3, 5) stars <- c("*", "***", "**") # 循环添加星号 for(i in seq_along(target_rows)){ grid.text( stars[i], x = unit(1.02, "npc"), # x坐标:稍超出热图右侧 y = unit(y_coords[target_rows[i]], "npc"), # 对应行的y坐标 gp = gpar(fontsize = 14, col = "red", fontface = "bold") ) }
注意事项
- 方法1更规范,适合批量处理所有行的标记;
- 方法2灵活性更高,可针对特定行调整星号的位置、大小和颜色;
- 无需依赖ggplot2,ComplexHeatmap本身基于grid生态,直接用相关工具即可完成需求。
内容的提问来源于stack exchange,提问作者Lili
相关产品推荐
相关产品推荐

