Home > Net >  Selecting the color of bars from a variable
Selecting the color of bars from a variable

Time:12-31

I want to set some previously selected colors to my barplot. For example, I want Ferrari to be red, Mercedes turquoise...

You can use, for example, the following simple dataset:

structure(list(points = c(3183, 27, 210, 334, 352, 898, 559, 812, 3796, 2508), name = c("Ferrari", 
"Williams", "Aston Martin", "Haas F1 Team", "AlphaTauri", "McLaren", 
"Alfa Romeo", "Alpine F1 Team", "Red Bull", "Mercedes"), color = c("red", "lightblue", "darkgreen", "darkred", "black", "grey", "darkblue", "pink", "yellow", "turquoise")), row.names = 1:9, class = "data.frame")

I tried to use the following code:

library(plotly)
library(dplyr)
plot_ly(data, x =~name, y=~points, color = ~name, colors = ~color)%>           
  • Related