条形图 R 中的颜色

Colors in plotly bar graph R

另见

在绘图条形图中使用颜色变量时。 如何更改默认配色方案?

    library(plotly)
    library(dplyr)

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

p

colors plot_ly 的参数采用类别长度的向量:

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