在 R 中的热图行上添加矩形

sha*_*own 1 r heatmap

我正在尝试在函数调用graphics::rect的一行周围添加矩形框(即),heatmap如下所示:

stats::heatmap(matrix(data = rnorm(25),nrow = 5,ncol = 5),Colv = NA,scale = "none")
Run Code Online (Sandbox Code Playgroud)

为了确定参数,rect我需要能够自动检索绘制的热图的 xlim 和 ylim (不是侧面的树状图),但我不知道该怎么做..限制似乎并不存在为 1:5(对于本例)。

r2e*_*ans 5

知道在哪里放置角度的技巧rect是获得正确的用户坐标。不幸的是,par("usr")(我最初的猜测)返回c(0, 1, 0, 1),没有帮助。然而,我通过阅读最后的“注释”中的内容找到了为什么会出现这种情况的线索?heatmap

\n
     \xe2\x80\x98heatmap()\xe2\x80\x99 uses layout and draws the image in the lower right\n     corner of a 2x2 layout.  Consequentially, it can *not* be used in\n     a multi column/row layout, i.e., when \xe2\x80\x98par(mfrow = *)\xe2\x80\x99 or \xe2\x80\x98(mfcol\n     = *)\xe2\x80\x99 has been called.\n
Run Code Online (Sandbox Code Playgroud)\n

这意味着在整个图完成后我们无法分解子图的用户坐标。然而......在同一个帮助页面中,我们看到

\n
add.expr: expression that will be evaluated after the call to image.\n          Can be used to add components to the plot.\n
Run Code Online (Sandbox Code Playgroud)\n

我相信“调用图像”意味着实际的矩阵颜色图。

\n

所以我尝试了这个:

\n
     \xe2\x80\x98heatmap()\xe2\x80\x99 uses layout and draws the image in the lower right\n     corner of a 2x2 layout.  Consequentially, it can *not* be used in\n     a multi column/row layout, i.e., when \xe2\x80\x98par(mfrow = *)\xe2\x80\x99 or \xe2\x80\x98(mfcol\n     = *)\xe2\x80\x99 has been called.\n
Run Code Online (Sandbox Code Playgroud)\n

好的,我们需要将您添加rect到通话中add.expr,坐标很直观。

\n
add.expr: expression that will be evaluated after the call to image.\n          Can be used to add components to the plot.\n
Run Code Online (Sandbox Code Playgroud)\n

带有矩形覆盖的基础 R 图形热图

\n

笔记:

\n
    \n
  • 左下角的左下角是,所以我们需要相应地0.5, 0.5偏移xsys
  • \n
  • xpd=NA就是停止“剪辑”;如果没有它,左/右矩形边框的一部分会被剪掉,因此会更薄;不是必需的,但一致
  • \n
\n
\n

数据

\n
stats::heatmap(mtx, Colv = NA, scale = "none", add.expr = { browser(); 1; })\nBrowse[4]> par("usr")\n[1] 0.5 5.5 0.5 5.5\n
Run Code Online (Sandbox Code Playgroud)\n