Use colors in sequence

I have a palette I need to use in sequence, i.e. when one category exists -> use first color, two categories use first color and second and so on.

How do I make sure this happens? Right now plotly is choosing element 5 of my color vector when I only have one category, for example.

chart_colors <- c("#FF4338", "#757C89", "#BCBF5E", "#C98989", "#F2BEBE",
                  "#D0D0E7", "#233EBE", "#f2bd94", "#4B4737", "#703D3A")

  plot_ly(data = plotData, x = ~balance_date, y = ~balance,
          color = ~account, colors = chart_colors,
          mode = "lines", showlegend = FALSE)

A quick solution:

chart_colors <- function(n_groups) {
  colors <- c("#FF4338", "#757C89", "#BCBF5E", "#C98989", "#F2BEBE",
              "#D0D0E7", "#233EBE", "#f2bd94", "#4B4737", "#703D3A")

  colors[1:n_groups]
}

But I would love to know if there’s a plotly way for doing this

@mariachi, you can use the colorway attribute to do this. See this doc for more info.

I tried and it does not give me the same effect as my solution above. Using colorway plotly is still choosing different colors depending on how many groups there are. For example, with only one group the color #FF4338 is not chosen.