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

运行数月的ggplot代码突发palette()报错,求助解决

问题描述

同一ggplot代码运行数月正常,今日突然报错:

Error in `palette()`: ! Insufficient values in manual scale. 19 needed but only 0 provided.

未修改数据框或代码,当前使用R版本2023.06.1。使用viridis调色板后出现该问题,数据框样本和代码如下:

数据框样本

df2 = structure(list(x = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                          1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                          1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                          1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("Method_Group", 
                                                                                              "Topic"), class = "factor"), node = c("BRUV + Both", "Behavioural Ecology", 
                                                                                                                                    "BRUV + Both", "Conservation Methods", "BRUV + Both", "Other Drivers", 
                                                                                                                                    "Animal Borne + No Receiver", "Behavioural Ecology", "Controlled + Receiver", 
                                                                                                                                    "Behavioural Ecology", "Controlled + Receiver", "Reproductive Ecology", 
                                                                                                                                    "Controlled + Receiver", "Other Drivers", "Controlled + Receiver", 
                                                                                                                                    "Behavioural Ecology", "Controlled + Receiver", "Methodological", 
                                                                                                                                    "Animal Borne + No Receiver", "Behavioural Ecology", "Animal Borne + No Receiver", 
                                                                                                                                    "Methodological", "Stationary + No Receiver", "Reproductive Ecology", 
                                                                                                                                    "Stationary + No Receiver", "Landuse Management", "Stationary + No Receiver", 
                                                                                                                                    "Other Drivers", "Animal Borne + No Receiver", "Behavioural Ecology", 
                                                                                                                                    "Animal Borne + No Receiver", "Methodological", "Animal Borne + No Receiver", 
                                                                                                                                    "Reproductive Ecology", "Stationary + Receiver", "Behavioural Ecology", 
                                                                                                                                    "Stationary + Receiver", "Fisheries Managemenet", "Stationary + Receiver", 
                                                                                                                                    "Behavioural Ecology", "Stationary + Receiver", "Methodological", 
                                                                                                                                    "Stationary + Receiver", "Fisheries Managemenet", "BRUV + Both", 
                                                                                                                                    "Behavioural Ecology", "BRUV + Both", "Methodological", "BRUV + Both", 
                                                                                                                                    "Conservation Methods"), next_x = structure(c(2L, NA, 2L, NA, 
                                                                                                                                                                                  2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
                                                                                                                                                                                  2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
                                                                                                                                                                                  2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA), levels = c("Method_Group", 
                                                                                                                                                                                                                                                      "Topic"), class = "factor"), next_node = c("Behavioural Ecology", 
                                                                                                                                                                                                                                                                                                 NA, "Conservation Methods", NA, "Other Drivers", NA, "Behavioural Ecology", 
                                                                                                                                                                                                                                                                                                 NA, "Behavioural Ecology", NA, "Reproductive Ecology", NA, "Other Drivers", 
                                                                                                                                                                                                                                                                                                 NA, "Behavioural Ecology", NA, "Methodological", NA, "Behavioural Ecology", 
                                                                                                                                                                                                                                                                                                 NA, "Methodological", NA, "Reproductive Ecology", NA, "Landuse Management", 
                                                                                                                                                                                                                                                                                                 NA, "Other Drivers", NA, "Behavioural Ecology", NA, "Methodological", 
                                                                                                                                                                                                                                                                                                 NA, "Reproductive Ecology", NA, "Behavioural Ecology", NA, "Fisheries Managemenet", 
                                                                                                                                                                                                                                                                                                 NA, "Behavioural Ecology", NA, "Methodological", NA, "Fisheries Managemenet", 
                                                                                                                                                                                                                                                                                                 NA, "Behavioural Ecology", NA, "Methodological", NA, "Conservation Methods", 
                                                                                                                                                                                                                                                                                                 NA)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                 ))

使用代码

library(viridis)
library(ggplot2)

levels(df2$node)
width <- .4

ggplot(df2, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = 1, node.color = "black", show.legend = FALSE, width = width) +
  theme_void() +
  theme(
    plot.margin = unit(rep(5.5, 4), "pt")
  ) +
  scale_fill_manual(values = viridis_pal()(length(levels(df2$node))))
解决思路
  • 核心原因:df2$node是字符型而非因子,levels(df2$node)返回NULL,导致length(levels(df2$node))=0,viridis调色板生成0个颜色,但绘图需要19个(factor(node)的实际水平数),因此报错。
  • 修正方案:
    1. 提前将node转为因子:
      df2$node <- factor(df2$node)
      
      再运行原代码,此时length(levels(df2$node))会正确返回19,匹配所需颜色数量。
    2. 直接修改颜色数量的计算方式:
      将scale_fill_manual中的参数改为:
      scale_fill_manual(values = viridis_pal()(length(unique(df2$node))))
      
      用unique(df2$node)获取实际的节点类别数,避免因变量类型导致的空值问题。
  • 额外排查项:
    • 检查viridis包是否有版本更新,新版本可能对空输入的处理更严格;
    • 重启R会话,仅加载ggplot2和viridis包再运行代码,排除其他包的冲突影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 10:44:56