Error with colors and named vector


I use the new Plotly (4.x) package for R with Shiny. I am colouring my graphs categorically, and if there is only 1 item in a category, the colouring exits with an error. A minimal example would be as follows:

plot_ly(head(iris, 1),

  • x=~Sepal.Length, y=~Sepal.Width, color=~Species,
  • colors=setNames("#FF5748", “setosa”),
  • type=“scatter”, mode=“markers”)

Error in if (has_attr(x$type, “colorscale”)) x[[“z”]] else NULL :
argument is of length zero

The colors vector is my named vector, where names refer to Species and the appropriate colour is chosen, always correct if more than one item per Species is present, e.g.:

x=~Sepal.Length, y=~Sepal.Width, color=~Species,
colors=setNames(c("#FF5748", “black”), c(“setosa”, “versicolor”)),
type=“scatter”, mode=“markers”)

Is this a plotly bug or am I missing something?


This is likely a bug. A workaround has been suggested on stackoverflow: