2

See also Custom colors in R Plotly

When using a variable for color in a plotly bar graph. How do you change the default color scheme?

    library(plotly)
    library(dplyr)

p <- ggplot2::diamonds %>% count(cut, clarity) %>%
  plot_ly(x = ~cut, y = ~n, color = ~clarity) %>%
  layout(barmode = "stack") 

p

enter image description here

Nina van Bruggen
  • 393
  • 2
  • 13
  • 1
    Does this answer your question? [custom colors in R Plotly](https://stackoverflow.com/questions/35601464/custom-colors-in-r-plotly) – s__ Dec 18 '19 at 13:19

1 Answers1

3

colors argument of plot_ly takes a vector of length numbers of categories:

p <- ggplot2::diamonds %>% count(cut, clarity) %>%
   plot_ly(x = ~cut, y = ~n, color = ~clarity, colors = rainbow(10)) %>%
   layout(barmode = "stack") 
p
Clemsang
  • 5,053
  • 3
  • 23
  • 41