I have a dataset that I want to turn into a plotly chart in R Shiny. I also have a long list of colors, but they need to be used for any category, but they must be used in order (e.g., the plot shouldn't use the 4th color unless the first 3 have been used and so on).
For some reason just for my bar charts (I am not having this issue with pie charts), plotly chooses the colors out of order. For instance, here is the data:
library(dplyr)
test <- tibble(project = c("big", "medium", "big", "medium"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
And here is the plot when I have all my colors:
all_colors <- c("#CA001B", "#1D28B0", "#D71DA4", "#00A3AD", "#FF8200", "#753BBD", "#00B5E2", "#008578", "#EB6FBD", "#FE5000", "#6CC24A", "#D9D9D6", "#AD0C27", "#950078")
library(plotly)
fig <- plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project, color = ~project, colors = all_colors) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), font = line_chart_text_format, barmode = "stack")
fig
But here's it looking correct when I only include the first 2 colors:
some_colors <- c("#CA001B", "#1D28B0")
library(plotly)
fig <- plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project, color = ~project, colors = some_colors) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), font = line_chart_text_format, barmode = "stack")
fig
How do I force plotly to use my colors in order? I don't want to map colors to specific variables because future visualizations may have more categories. For that same reason, I don't want to just include the minimum number of viable colors.