0

I would like to color each bar according to the value on a red scale. For example, the largest bar would be black rgb(0,0,0), the smallest would be rgb(80,0,0) and the intermediate ones would vary in intensity.

The link below has a solution in python, I would like to do it in re and using plotly

Changing color scale in seaborn bar plot

library(plotly)

city   <- c("Paris", "New York", "Rio", "Salvador", "Curitiba", "Natal")
value  <- c(10,20,30,10,10,10)

data   <- data.frame(city, value, stringsAsFactors = FALSE)

data <- data[order(-data$value, data$city),]

data$city <- factor(data$city, levels = unique(data$city)[order(data$value, decreasing = FALSE)])


fig <- plot_ly(y = reorder(data$city,-data$value), x = data$value, type = "bar", orientation = 'h') %>% layout(yaxis = list(autorange = "reversed"))
fig
1
  • I would like to do it in R Commented Apr 30, 2020 at 0:46

1 Answer 1

2

If I were you, I'd use the ggplot2 package in R. You can feed ggplot plots directly into Plotly.

See cheat sheet here: https://github.com/rstudio/cheatsheets/blob/master/data-visualization-2.1.pdf

There is a function scale_fill_gradient() where you can set your low and high values. It would work perfectly for your purposes.

Sign up to request clarification or add additional context in comments.

1 Comment

I had a little difficulty, but I managed to use ggplot in conjunction with plotly, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.