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

在RStudio中使用geom_tile生成空白热力图的问题排查

解决ggplot2绘制NMI热力图空白问题

问题描述

我是R编程新手,尝试可视化不同聚类算法得到的簇之间的Normalised Mutual Information(归一化互信息)。目标是生成类似ggcorrplot样式的热力图,但由于数据并非相关系数,ggcorrplot无法使用,因此尝试用ggplot2手动绘制。

手动创建的数据框如下:

Nmi_frame <- data.frame(
  "mesure1" = c("fg","fg","fg","fg","fg","fg","fg",
                "sp","sp","sp","sp","sp","sp","sp",
                "wt","wt","wt","wt","wt","wt","wt",
                "lv","lv","lv","lv","lv","lv","lv",
                "sg","sg","sg","sg","sg","sg","sg",
                "ca","ca","ca","ca","ca","ca","ca",
                "cf","cf","cf","cf","cf","cf","cf"),

  "mesure2"= c( "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf",
                 "fg","sp","wt","lv","sg","ca","cf"),

  "nmi_value" = c(1,0.3671792,0.3260069,0.2499102,0.2410074,0.2586295,0.1722615,
                  0.3671792,1,0.5884902,0.4380218,0.3148344,0.2399497,0.3783691,
                  0.3260069, 0.5884902, 1,0.474305,0.3441412,0.2264623,0.3448708,
                  0.2499102,0.4380218, 0.474305, 1,0.5431077,0.1560136,0.2078255,
                  0.2410074 ,0.3148344, 0.3441412, 0.5431077,1, 0.1661684,0.1880403,
                  0.2586295,0.2399497, 0.2264623,0.1560136, 0.1661684, 1,0.2315498,
                  0.1722615, 0.3783691, 0.3448708, 0.2078255, 0.1880403, 0.2315498, 1))

执行以下绘图代码后仅得到空白图:

ggplot(Nmi_frame, aes(x = mesure1, y = mesure2, fill = nmi_value),
        geom_tile(color = "white", aes(fill = Nmi_frame$nmi),
          scale_fill_gradient(low = "blue", high = "red")))

已尝试将前两列转为因子,但结果仍无改善。请问该如何解决此问题?是否为ggplot的bug?


问题原因与解决方法

这不是ggplot的bug,完全是代码语法错误导致的:

  • 图层参数位置错误:geom_tile和scale_fill_gradient是独立的图层/标度函数,不能作为ggplot()的参数传入,必须用+符号连接在ggplot()之后,这是ggplot2的核心语法规则。
  • 映射变量错误:aes(fill = Nmi_frame$nmi)中,Nmi_frame$nmi不存在,你的数据框里对应的列名是nmi_value;而且在ggplot的映射中不需要加数据框前缀,直接用列名即可。
  • 冗余映射:你已经在ggplot()的全局aes里指定了fill = nmi_value,不需要在geom_tile里重复指定fill映射。

正确代码示例

library(ggplot2)

ggplot(Nmi_frame, aes(x = mesure1, y = mesure2, fill = nmi_value)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "blue", high = "red") +
  # 可选:添加数值标签,让热力图更直观
  geom_text(aes(label = round(nmi_value, 2)), color = "white", size = 3) +
  labs(title = "不同聚类算法的归一化互信息热力图",
       x = "聚类算法", y = "聚类算法", fill = "NMI值") +
  theme_minimal()

代码说明

  1. 用+连接各个组件:ggplot()定义基础数据和映射,后续用+添加geom_tile(绘制色块)、scale_fill_gradient(设置颜色渐变)等。
  2. 全局映射复用:geom_tile直接继承ggplot()里的x、y、fill映射,只需要补充边框颜色参数。
  3. 可选优化:添加geom_text显示NMI数值,用labs设置标题和标签,theme_minimal让图表更简洁美观。

内容的提问来源于stack exchange,提问作者user_s_moon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 10:10:24