Search code examples
rcolorsplotlybar-chart

R plotly: bar plot with rainbow like gradient color across bars


I would like to create a plot like below using plotly

plot example

Specifically I would like the color scale goes from green to read with gradient change. I also want a side color bar indicating the value range. The values of the x axis range from 0 to 1. Bars for smaller values are closed to green. Bars with lager values gradually change to red.

Below is my current code

library(dplyr)
library(plotly)
library(tibble)

# create data for testing
make_test_data <- function(n_row) tibble(ID = stringr::str_pad(seq(n_row), 6, pad = '0'),
                                                 parameter = stats::runif(n_row))
df_test <- make_test_data(30)

# draw bar plot with plot_ly
plot_ly(df_test, x = ~parameter, y = ~ID, type = 'bar', orientation = 'h') %>% 
  layout(xaxis = list(title = 'Parameter Name (Unit)', range = c(0, 1), tickvals = seq(0, 1, 0.2)), 
         yaxis = list(title = 'Sample ID', categoryorder = "total ascending"))

This just give me bar plot with a single default color. I then tried the following according to the answer of another question.

plot_ly(df_test, x = ~parameter, y = ~ID, type = 'bar', orientation = 'h',
        colorscale = cbind(seq(0, 1, by=1/(length(~ID) - 1)), rainbow(length(~ID)))) %>% 
  layout(xaxis = list(title = 'Parameter Name (Unit)', range = c(0, 1), tickvals = seq(0, 1, 0.2)), 
         yaxis = list(title = 'Sample ID', categoryorder = "total ascending"))

But I got the warning below and the same single color bars. Seems bar type does not support colorscale. Is there any workaround? I need a solution using solely plot_ly, not ggplotly + ggplot.

Warning message:
'bar' objects don't have these attributes: 'colorscale'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiod [... truncated] 

Solution

  • You didn't assign the color to a variable. Additionally, if you're using colorscale, I'm pretty sure it needs to be nested in marker.

    This code has a lot of callouts for the colorbar, so that it closely resembles the plot you are trying to emulate. Additionally, I used set.seed() so I would be able to have repeatable results.

    library(dplyr)
    library(plotly)
    library(tibble)
    
    # create data for testing
    set.seed(539)
    make_test_data <- function(n_row) tibble(ID = stringr::str_pad(seq(n_row), 6, pad = '0'),
                                             parameter = stats::runif(n_row))
    df_test <- make_test_data(30)
    
    # draw bar plot with plot_ly
    plot_ly(df_test, x = ~parameter, y = ~ID, type = 'bar', orientation = 'h',
            marker = list(colorscale = list(c(0,1), c("lawngreen", "red")),
                          colorbar = list(title = "Parameter Name (Unit)",
                                          len = .4, outlinewidth = 0,
                                          tickformat = ".2f",
                                          tick0 = 0, dtick = .25),
                          color = ~parameter)) %>% 
      layout(xaxis = list(title = 'Parameter Name (Unit)', 
                          range = c(0, 1), tickvals = seq(0, 1, 0.2)), 
             yaxis = list(title = 'Sample ID', categoryorder = "total ascending"))
    

    enter image description here