R语言绘制自定义参数红色系代谢物相关性热图求助
R语言符合要求的热图绘制方案
采用ComplexHeatmap包实现所有定制化需求,代码可直接运行,适配大规模数据集。
第一步:安装加载依赖包
首次运行需先安装依赖,后续使用直接加载即可:
# 首次使用执行安装 if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("ComplexHeatmap") install.packages(c("circlize", "tidyverse")) # 加载包 library(ComplexHeatmap) library(circlize) library(tidyverse)
第二步:数据预处理
将输入数据转为热图要求的矩阵格式,同时配置Y轴亚部分组信息:
# 加载示例数据 data.str <- structure( list( Metabolite = c( "Glucose_%", "Fructose_%", "Sugars_%", "Sugars.as.mono_%", "Starch_%", "Starch.as.mono_%", "Glutamic_%", "Proline_%", "Biotin_%", "C20.2cn6_%" ), Difference_S_HDL_CE_pct = c( 27.0580967, 29.4741588, 30.6727965, 37.044534, 0.00592, 0.00924, 2.8089622, 4.3284966, 3.616572, 16.6234106 ), Difference_L_HDL_TG = c( 20.3426932, 19.6578323, 6.1540709, 6.618907, 20.7397107, 22.7363078, 5.7014121, 6.0341221, 5.9002733, 5.0788391 ), Difference_Gln = c( 20.2203384, 21.9353406, 17.1303398, 17.3148438, 8.7804598, 7.0612042, 0.5848933, 0.3856946, 0.0748362, 10.761994 ), Difference_S_LDL_FC_pct = c( 20.1171041, 21.1194979, 19.0084278, 22.7941105, 0.6624931, 1.2516315, 0.4276689, 1.3559095, 1.7227713, 18.2480865 ), Difference_Pyruvate = c( 17.5854511, 19.9410449, 12.7105925, 11.9623687, 10.4788242, 8.6559229, 0.0189435, 0.0239544, 0.00312, 16.7369868 ), Difference_L_LDL_TG = c( 13.250508, 13.9865028, 13.4782952, 14.1116228, 4.0569956, 3.3975936, 0.8144706, 0.7677175, 0.0572463, 14.4629904 ), Difference_S_HDL_C_pct = c( 12.9346568, 13.1410302, 16.6905244, 22.6788084, 1.7590544, 1.3980602, 2.9658478, 4.8456946, 2.7936213, 27.68029 ), Difference_S_HDL_PL_pct = c( 12.864684, 11.9193723, 10.8118128, 11.8431789, 1.0692856, 1.4855054, 1.9670692, 1.3530411, 8.5227632, -1.6739852 ), Difference_M_LDL_TG_pct = c( 12.8530009, 11.4667269, 13.5677286, 16.3305456, 0.9506285, 0.5322617, 0.8908791, 1.898937, 2.9490729, 29.5990618 ), Difference_L_LDL_C_pct = c( 12.5509812, 13.624991, 12.482877, 13.1641219, 0.3197067, 0.0195839, 8.7725257, 7.2044468, 0.0122509, 8.4407425 ), Difference_IDL_TG = c( 12.0014, 11.378059, 6.0707129, 6.2593626, 8.3150096, 9.6032892, 4.2182798, 3.8636934, 4.2984429, 1.8889026 ) ), row.names = c(NA, 10L), class = "data.frame" ) # 转为热图矩阵:行=Y轴代谢物差异指标,列=X轴营养物指标 heat_mat <- data.str %>% column_to_rownames("Metabolite") %>% as.matrix() %>% t() # 按实际需求修改Y轴亚部分组,示例分组如下 row_group <- c( rep("脂质类指标", 9), rep("氨基酸类指标", 2) ) # 亚组配色可自行调整 group_col <- c( "脂质类指标" = "#fcbba1", "氨基酸类指标" = "#fb6a4a" )
第三步:配置热图核心参数
完全匹配需求设置:
- 色阶范围固定为-2~45
- 红色系渐变,数值越高颜色越深
- 仅数值大于5的单元格显示对应数值
# 配置红色系色阶 col_fun <- colorRamp2( breaks = c(-2, 20, 45), colors = c("#fff5f0", "#fc9272", "#67000d") ) # 配置单元格显示文本 cell_label <- matrix("", nrow = nrow(heat_mat), ncol = ncol(heat_mat)) cell_label[heat_mat > 5] <- round(heat_mat[heat_mat > 5], 1)
第四步:绘制热图
Heatmap( matrix = heat_mat, name = "差异值", col = col_fun, # 聚类开关,需要排序聚类改为TRUE即可 cluster_rows = FALSE, cluster_columns = FALSE, # 单元格文本 cell_fun = function(j, i, x, y, width, height, fill) { grid.text(cell_label[i,j], x, y, gp = gpar(fontsize = 8)) }, # Y轴亚组注释 left_annotation = rowAnnotation( 指标分类 = row_group, col = list(指标分类 = group_col) ), # 样式调整 rect_gp = gpar(col = "white", lwd = 1), row_names_gp = gpar(fontsize = 9), column_names_gp = gpar(fontsize = 9), column_names_rot = 45 )
调整说明
- 替换自己的完整数据集时,保持和示例数据结构一致即可:第一列为营养物名称,其余列为代谢物差异指标,代码会自动适配规模
- Y轴亚组的分类、颜色、数值显示阈值、色阶深浅都可以直接在对应代码段修改
- 开启行/列聚类时,亚组注释会自动跟随行顺序排列,不需要额外调整
内容的提问来源于stack exchange,提问作者Ram6
相关产品推荐
相关产品推荐

