Stacked bar chart - specify color platte for a column

Hi, I want to use plotly to display different shades of red depending on the category column.

grp <- c(“A”, “B”, “C”, “D”)
xval <- as.factor(c(‘x’, ‘y’, ‘x’, ‘y’))
frame <- merge(grp, xval, all=T)
yval <- as.factor(c(‘low’, ‘low-medium’, ‘medim-high’, ‘high’))
df <- tbl_df(cbind(frame, yval))
colnames(df) <- c(“category”, “Type”, “Range”)

df %>% group_by(category) %>% arrange(Type) %>%
plot_ly( x = ~Type, y = ~Range, color= ~category, type = ‘bar’
)%>%
layout(yaxis = list(title = ‘value’), barmode = ‘stack’)

below is the result:

How can I change the color to be a range of red? I tried to define the colors i wanted:
colors <- c( ‘rgb(252,174,145)’, ‘rgb(251,106,74)’, ‘rgb(222,45,38)’, ‘rgb(165,15,21)’)
But it didn’t work.
Can anyone help please? I want to the colors for each bar to be like this:

anyone can help at all?:confused:

hey @gcmicah

do you mean like this?

df %>% 
  group_by(category) %>% 
  arrange(Type) %>%
  plot_ly(
    x = ~Type, 
    y = ~Range, 
    color= ~category,
    colors = 'Reds',
    type = 'bar') %>% 
  layout(
    yaxis = list(title = 'value'), 
    barmode = 'stack')

Hi @bcd, thanks for reply. it is what I wanted. :grinning::+1:

Hello @bcd, an extended question. How can I reverse the color of the shades of reds?
The output of your modified code is:


How can I make the darkest red at the bottom and the shades go darker on the top? that is, medium-high is still shown as dark red but it’s at the bottom of the stacked bar. Thank you.