几个月来,我一直在运行相同的代码,没有任何问题.今天,当我运行它时,我得到了错误
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.