I have this plot:
Especie <- c("Perros","Gatos","Pájaros","Total")
Cantidad <- c(3,2,4,9)
df <- data.frame(Especie,Cantidad)
grafico <- plot_ly(df,
x = ~Especie,
y = ~Cantidad,
type = "bar")
Is it possible to conditionally get the "Total" bar to be painted a different color, based on that particular value on the ‘Especie’ column?
Alternatively, is it posible to get the bar with the highest value on ‘Cantidad’ to be painted a different color?
Thanks!
>Solution :
Another option using an conditional statement ifelse to your marker of colors like this:
library(plotly)
grafico <- plot_ly(df,
x = ~Especie,
y = ~Cantidad,
type = "bar",
marker = list(color = ifelse(df$Especie == 'Total', "red", "blue")))
grafico

Created on 2023-03-28 with reprex v2.0.2