I would like to create a plot like below using plotly
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]

