You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中并排组合树状图与heatmap.2热力图的问题排查

解决方案:将进化树与heatmap.2热力图并排展示

问题背景

你有两段R代码分别生成进化树和热力图:

  • 进化树绘制代码:
plot(tree, show.tip.label = TRUE, direction = "rightwards", cex = 0.7)
  • 热力图绘制代码:
heatmap.2(as.matrix(reordered_matrix), Rowv = FALSE, Colv = FALSE, dendrogram = "none", trace = "none", key = TRUE, symm = TRUE, col = bluered(100), margins = c(5, 5))

想要实现二者并排展示的布局,但使用par(mfrow=c(1,2))尝试时,绘制热力图后进化树会消失——问题根源是heatmap.2会自动重置全局绘图参数,覆盖了之前的布局设置。

可行解决方案

改用layout()自定义绘图区域,并给heatmap.2添加参数阻止其重置绘图环境,具体代码如下:

# 定义1行2列的布局,调整左右区域宽度比例(可按需修改)
layout(matrix(c(1,2), nrow=1), widths=c(3,7))

# 绘制左侧进化树,设置合适边距
par(mar=c(5,4,4,0))
plot(tree, show.tip.label = TRUE, direction = "rightwards", cex = 0.7)

# 绘制右侧热力图,核心参数add.expr阻止重置布局
par(mar=c(5,0,4,2))
heatmap.2(as.matrix(reordered_matrix),
          Rowv = FALSE, Colv = FALSE, dendrogram = "none", 
          trace = "none", key = TRUE, symm = TRUE, 
          col = bluered(100), margins = c(5, 5),
          add.expr = par(new = FALSE))

补充说明

  • layout()的widths参数可灵活调整左右区域的宽度占比,适配图形显示需求;
  • add.expr = par(new = FALSE)用于抵消heatmap.2内部的参数重置逻辑,保留当前布局环境;
  • 若需要更灵活的图形拼接,也可以选择ggtree(绘制进化树)+pheatmap(绘制热力图)+patchwork(拼接图形)的组合方案,适配ggplot2生态的绘图需求。

内容的提问来源于stack exchange,提问作者Mohamed Aziz Hadj Nasr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:06:08