Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change color to only one bar on plotly bar plot?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading