Create a plotly bar chart with different colors for each bar

Viewed 313

How can I create a bar chart with each bar colored differently and also add a legend with the name of each bar and the color used (not the name of the color,the color itself).

library(plotly)

fig <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar"
)

fig
1 Answers

There is a color argument

fig <- plot_ly(
    x = c("giraffes", "orangutans", "monkeys"),
    y = c(20, 14, 23),
    name = c("giraffes", "orangutans", "monkeys"),
    type = "bar", color = c('red', 'blue', 'green')
 )
fig
Related