I have been running this same code for months with no issues. And today when I ran it I got the error
Error in `palette()`: ! Insufficient values in manual scale. 19 needed but only 0 provided. I'm not sure why I am getting this now because I haven't changed anything in my dataframe or in my code.
Here is a sample of my dataframe
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"
))
and this is the code I am using
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))))
typically I would fix this my making sure the number of colours I specified is the same number as the variables I am graphing. But since I'm using viridis I'm not really sure why that would affect it. Any ideas how to fix this? Also, not sure if it makes a difference but currently using the R version 2023.06.1.
In your data frame, the column
nodeis a character vector, not a factor:This means that
length(levels(df2$node))is 0.So your call to
viridis_pal()is producing a zero-length vector of colors:All you need to do is to convert
df2$nodeto a factor and everything should work as expected. Note thatnlevels(factor(df2$node))is a quicker way of writinglength(levels(factor(df2$node)))