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

R中scale_fill_gradientn函数报错:向连续色阶传入离散值

问题分析与解决方案

报错原因

报错Discrete values supplied to continuous scale的核心问题是:你的number列是字符型(字符串),但scale_fill_gradientn是为连续数值型变量设计的颜色映射函数,两者类型不匹配,因此触发错误。报错信息里的"0"带引号,也直接佐证了这一点——如果是数值型,不会显示引号。

解决方案

1. 转换number列为数值型

在绘图前,先将number列强制转换为数值类型,使用as.numeric()函数:

# 转换数据类型(关键步骤)
data$number <- as.numeric(data$number)

如果转换时出现警告(比如数据中存在非数字字符),可以用which(!grepl("^\\d+$", data$number))定位异常值,清理后再执行转换。

2. 修复后的完整代码

将类型转换步骤加入原代码,同时可添加na.value参数处理转换中可能出现的缺失值:

library(ggplot2)

colors <- c("#f7f9cb", "#c7edc0", "#92e0c3", "#56d0d0", "#00bdde", "#00afef", "#539df4", "#9085e8", "#cd6ace", "#f54a9d", "#ff3d5e", "#f0550d", "#fc8d59")

# 转换number列为数值型
data$number <- as.numeric(data$number)

ggplot(data, aes(x = tm, y = characteristic, fill = number)) +
  geom_tile(color = "black", size = 0.2) +
  geom_text(aes(label = ifelse(number != 0, number, "")), size = 3) +
  scale_fill_gradientn(colors = colors, na.value = "white") + # 处理NA值
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
        panel.border = element_rect(color = "black", fill = NA, size = 1),
        axis.line = element_line(color = "black"),
        plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
        axis.title = element_text(face = "bold", size = 12),
        axis.text = element_text(size = 10, face = "bold"),
        legend.title = element_text(face = "bold", size = 12),
        legend.text = element_text(size = 10, face = "bold"),
        legend.box.background = element_rect(color = "black", size = 1)) +
  labs(title = "Heatmap of Amino Acid Properties vs Inter-TM",
       x = "Transmembrane Regions",
       y = "Properties",
       fill = "Number of \nResidual Pairs") +
  coord_fixed()

绘制热图的替代方法

方法1:使用pheatmap包(静态专业热图)

pheatmap专为热图设计,适合矩阵形式的数据,需先将长格式数据转换为宽矩阵:

library(pheatmap)
library(tidyr)

# 转换为宽格式矩阵
heat_matrix <- pivot_wider(data, 
                           id_cols = characteristic, 
                           names_from = tm, 
                           values_from = number,
                           values_fill = 0) %>%
  column_to_rownames("characteristic") %>%
  as.matrix()

# 绘制热图
pheatmap(heat_matrix,
         color = colors,
         display_numbers = TRUE, # 显示单元格数值
         number_color = "black",
         border_color = "black",
         main = "Heatmap of Amino Acid Properties vs Inter-TM",
         fontsize_row = 10,
         fontsize_col = 10,
         treeheight_row = 0, # 关闭行聚类
         treeheight_col = 0) # 关闭列聚类

方法2:使用heatmaply包(交互式热图)

适合需要鼠标悬停查看细节的场景,生成可交互的网页版热图:

library(heatmaply)
library(tidyr)

# 转换为宽格式矩阵
heat_matrix <- pivot_wider(data, 
                           id_cols = characteristic, 
                           names_from = tm, 
                           values_from = number,
                           values_fill = 0) %>%
  column_to_rownames("characteristic") %>%
  as.matrix()

# 绘制交互式热图
heatmaply(heat_matrix,
          colors = colors,
          showticklabels = c(TRUE, TRUE),
          main = "Heatmap of Amino Acid Properties vs Inter-TM",
          fontsize_row = 10,
          fontsize_col = 10)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 23:30:24