如何在ComplexHeatmap中为行名添加上标并修改行注释图例标题?
ComplexHeatmap热图的两个需求实现方案
问题描述
我使用以下R代码生成了ComplexHeatmap热图:
A = c(rep(100,22),rep(0,15),rep(10,12), rep(50,14)) B = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14)) C = c(rep(0,22),rep(8,15),rep(13,6),rep(75,6), rep(82,14)) D = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14)) E = c(rep(100,22),rep(0,15),rep(10,12), rep(50,14)) F = c(rep(0,22),rep(8,15),rep(13,6),rep(75,6), rep(82,14)) G = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14)) H = c(rep(80,22),rep(72,15),rep(5,12), rep(2,14)) df= data.frame(A,B,C,D,E,F,G, H) correct_columnnames <- c("34T","56G", "45T","78T", "12F","25G","90F","78T") colnames(df) <- correct_columnnames split = c(rep(1,22),rep(2,15),rep(3,12), rep(4,14)) row_labels = c(rep("A",22),rep("B",15),rep("C",12), rep("D",14)) rowlabels= data.frame(row_labels) row_labels = rowAnnotation(df = rowlabels, col = list(row_labels = c("A"="#e48f1c","B"="#570c32","C"="#e5c616","C"="#d33b44", "D" = "#38ECF9")), annotation_legend_param = list(direction = "horizontal", nrow = 1, title = "Groups", title_position = "topcenter", legend_gp = gpar(fontsize = 14), labels_gp = gpar(fontsize = 14)), gp = gpar(col = "grey") ) h1 = Heatmap(df, clustering_method_columns = "average", rect_gp = gpar(col = "grey", lwd = 1), right_annotation = row_labels, cluster_rows = FALSE, heatmap_legend_param = list(legend_direction = "horizontal", title = "M porcentage", legend_width = unit(11, "cm"), title_position = "topcenter", at = c(0,10,20,30,40,50,60,70,80,90,100), labels = c(0,10,20,30,40,50,60,70,80,90,100), fontsize = 16), width = ncol(filtered_data)*unit(5, "mm"), height = nrow(filtered_data)*unit(5, "mm"), column_split = 7, column_title = NULL, row_split = split, row_title = NULL) png("test.png",width=20,height=20, res = 300, "in") draw(h1, heatmap_legend_side = "bottom", annotation_legend_side = "bottom" ) dev.off()
当前热图效果:
需要实现两个需求:
- 为热图的行名添加部分上标字符;
- 将行注释的图例标题修改为“行标签”(对应预期效果中的“row labels”)。
预期热图效果:
解决方案
关键修改点
- 行名添加上标:借助
grid包的expression语法构造带标的行名,在Heatmap函数中通过row_names参数指定。 - 修改注释图例标题:直接修改
rowAnnotation中annotation_legend_param的title参数为目标文本。 - 修复原代码中未定义的
filtered_data变量,以及png函数的语法错误。
修改后的完整代码
library(ComplexHeatmap) library(grid) # 构造数据 A = c(rep(100,22),rep(0,15),rep(10,12), rep(50,14)) B = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14)) C = c(rep(0,22),rep(8,15),rep(13,6),rep(75,6), rep(82,14)) D = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14)) E = c(rep(100,22),rep(0,15),rep(10,12), rep(50,14)) F = c(rep(0,22),rep(8,15),rep(13,6),rep(75,6), rep(82,14)) G = c(rep(0,22),rep(8,15),rep(13,12), rep(82,14)) H = c(rep(80,22),rep(72,15),rep(5,12), rep(2,14)) df= data.frame(A,B,C,D,E,F,G, H) correct_columnnames <- c("34T","56G", "45T","78T", "12F","25G","90F","78T") colnames(df) <- correct_columnnames split = c(rep(1,22),rep(2,15),rep(3,12), rep(4,14)) # 构造带标的行名,可根据需求修改上标内容 row_names <- c(rep(expression(A^1),22), rep(expression(B^2),15), rep(expression(C^3),12), rep(expression(D^4),14)) rowlabels= data.frame(row_labels = c(rep("A",22),rep("B",15),rep("C",12), rep("D",14))) # 修改行注释图例标题为“行标签” row_labels = rowAnnotation(df = rowlabels, col = list(row_labels = c("A"="#e48f1c","B"="#570c32","C"="#d33b44", "D" = "#38ECF9")), annotation_legend_param = list(direction = "horizontal", nrow = 1, title = "行标签", title_position = "topcenter", legend_gp = gpar(fontsize = 14), labels_gp = gpar(fontsize = 14)), gp = gpar(col = "grey") ) h1 = Heatmap(df, clustering_method_columns = "average", rect_gp = gpar(col = "grey", lwd = 1), right_annotation = row_labels, cluster_rows = FALSE, heatmap_legend_param = list(legend_direction = "horizontal", title = "M porcentage", legend_width = unit(11, "cm"), title_position = "topcenter", at = c(0,10,20,30,40,50,60,70,80,90,100), labels = c(0,10,20,30,40,50,60,70,80,90,100), fontsize = 16), width = ncol(df)*unit(5, "mm"), height = nrow(df)*unit(5, "mm"), column_split = 7, column_title = NULL, row_split = split, row_title = NULL, row_names_gp = gpar(fontsize = 10), # 可调整行名字体大小 row_names = row_names) # 指定带标的行名 # 修正png函数参数语法 png("test.png", width=20, height=20, res=300, units="in") draw(h1, heatmap_legend_side="bottom", annotation_legend_side="bottom") dev.off()
补充说明
- 行名的上标内容可灵活调整,例如用
expression(A^"*")添加星号上标,或expression(A^"abc")添加字符上标; - 若不需要统一上标,可单独为特定行设置自定义上标格式。
内容的提问来源于stack exchange,提问作者Juliana B C
相关产品推荐
相关产品推荐

