Marcelo Rodrigues
Marcelo Rodrigues

Reputation: 259

Bar plot in red scale

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

Upvotes: 0

Views: 122

Answers (1)

Angela C
Angela C

Reputation: 176

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.

Upvotes: 2

Related Questions