r情节:带有彩虹的酒吧图,就像横跨条形的梯度颜色
我想使用plotly
具体来说,我希望颜色尺度从绿色到渐变变化。我还想要一个指示值范围的侧色条。 x
轴的值范围从0
到1
。较小值的条形接合到绿色。具有啤酒值的条逐渐变为红色。
下面是我当前的代码
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"))
,这只需给我带有单个默认颜色的栏图即可。然后,我根据另一个问题的答案。
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"))
但是我在下面得到了警告,并有相同的单色条。似乎条类型不支持colorscale
。有解决方法吗?我需要一个完全使用plot_ly
,不是 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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将颜色分配给变量。此外,如果您使用
colorscale
,我敢肯定它需要嵌套在标记
中。此代码对
colorbar
有很多标注,因此它非常类似于您试图模拟的绘图。此外,我使用了set.seed()
,因此我将能够获得可重复的结果。You didn't assign the color to a variable. Additionally, if you're using
colorscale
, I'm pretty sure it needs to be nested inmarker
.This code has a lot of callouts for the
colorbar
, so that it closely resembles the plot you are trying to emulate. Additionally, I usedset.seed()
so I would be able to have repeatable results.