几个月来,我一直在运行相同的代码,没有任何问题.今天,当我运行它时,我得到了错误 Error in `palette()`: ! Insufficient values in manual scale. 19 needed but only 0 provided.我不确定为什么现在会收到这个消息,因为我没有更改数据帧或代码中的任何内容.

这是我的数据帧示例

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))))

通常,我会通过确保我指定的 colored颜色 数量与我正在绘制的变量的数量相同来解决这个问题.但由于我使用的是viridis,我真的不确定这会对它有什么影响.有什么办法解决这个问题吗?此外,不确定这是否有影响,但目前使用的是R版本2023.06.1.

推荐答案

在数据框中,列node是一个特征向量,而不是一个因子:

df2
#> # A tibble: 50 x 4
#>    x            node                       next_x next_node           
#>    <fct>        <chr>                      <fct>  <chr>               
#>  1 Method_Group BRUV + Both                Topic  Behavioural Ecology 
#>  2 Topic        Behavioural Ecology        NA     NA                  
#>  3 Method_Group BRUV + Both                Topic  Conservation Methods
#>  4 Topic        Conservation Methods       NA     NA                  
#>  5 Method_Group BRUV + Both                Topic  Other Drivers       
#>  6 Topic        Other Drivers              NA     NA                  
#>  7 Method_Group Animal Borne + No Receiver Topic  Behavioural Ecology 
#>  8 Topic        Behavioural Ecology        NA     NA                  
#>  9 Method_Group Controlled + Receiver      Topic  Behavioural Ecology 
#> 10 Topic        Behavioural Ecology        NA     NA                  
#> # i 40 more rows
#> # i Use `print(n = ...)` to see more rows

这意味着length(levels(df2$node))是0.

length(levels(df2$node))
#> [1] 0

所以你对viridis_pal()的调用产生了一个零长度的 colored颜色 向量:

viridis_pal()(length(levels(df2$node)))
#> character(0)

你所需要做的就是将df2$node转换为一个因子,一切都应该按预期工作.注意nlevels(factor(df2$node))是写length(levels(factor(df2$node)))的更快的方法

library(ggsankey)
library(ggplot2)

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 = 0.4) +
  theme_void() +
  theme(plot.margin = unit(rep(5.5, 4), "pt")) +
  scale_fill_manual(values = viridis_pal()(nlevels(factor(df2$node))))

R相关问答推荐

如何在ggplot 2 geom_segment图表中将UTC转换为EET?

使用rlang s arg_match判断函数输入列表

使用ggplot 2根据R中的类别排列Likert比例gplot

根据收件箱中的特定值提取列名

使用R的序列覆盖

工作流程_set带有Dplyrr风格的 Select 器,用于 Select 结果和预测因子R

将包含卷的底部25%的组拆分为2行

如何得到每四个元素向量R?

在R中按行按列范围查找最大值的名称

查找所有站点的最小值

使用列中的值来调用函数调用中应使用的其他列

使用geom_sf跨越日期线时的闭合边界

R:使用ApexCharge更改标签在饼图中的位置

Data.table::Shift type=允许扩展数据(&Q;LAG&Q;)

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误

如何将字符类对象中的数据转换为R中的字符串

如何为包创建自定义roxygen2标签?

如何在R中的两列以上使用联合(&U)?

从两个数据帧中,有没有办法计算R中一列的唯一值?

使用nls()函数的非线性模型的半正态图