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

如何移除heatmap3热图图例并将热图移至绘图中心?

问题1:heatmap3热图居中调整

移除图例后,heatmap3默认布局仍为图例预留了空间,导致热图偏移。可以通过修改lmat、lhei、lwid参数重新定义布局,让热图占据整个绘图区域:

heatmap3::heatmap3(
  mtcars, 
  Colv = NA, 
  Rowv = NA, 
  legendfun = function() plot.new(),
  lmat = matrix(1, nrow = 1, ncol = 1),  # 仅保留热图区域
  lhei = c(1),  # 高度占比100%
  lwid = c(1)   # 宽度占比100%
)

lmat是布局矩阵,1代表热图区域;lhei和lwid分别设置各区域的高度和宽度比例,设为c(1)即让热图占满整个画布,实现居中效果。

问题2:tidyverse绘制热图的报错解决

报错! ... must be empty. ✖ Problematic argument: • call = call是由于tidyr与vctrs/tibble包版本不兼容导致的,解决方法如下:

方法1:更新相关包

运行以下命令更新tidyverse及依赖包:

install.packages("tidyverse")

更新后重新运行代码即可解决版本冲突问题。

方法2:使用替代函数(无需更新)

如果无法更新包,可以用已弃用但仍可用的gather()替代pivot_longer():

library(ggplot2)
library(tidyverse)

mtcars |>
  rownames_to_column() %>%
  gather(key = name, value = value, -rowname) %>%  # 替换pivot_longer
  ggplot(aes(factor(name, unique(name)), rowname, fill = value)) +
  geom_tile() +
  scale_fill_gradientn(colours = c("blue4", "white", "red3")) +
  scale_y_discrete(position = "right") +
  theme(legend.position = "none", 
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        text = element_text(face = 2))

内容的提问来源于stack exchange,提问作者J.Z.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 07:15:57