Search code examples
rggplot2palette

New ggplot Error in `palette()` with code I have been running for months


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.


Solution

  • In your data frame, the column node is a character vector, not a factor:

    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
    

    This means that length(levels(df2$node)) is 0.

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

    So your call to viridis_pal() is producing a zero-length vector of colors:

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

    All you need to do is to convert df2$node to a factor and everything should work as expected. Note that nlevels(factor(df2$node)) is a quicker way of writing 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))))