r情节:带有彩虹的酒吧图,就像横跨条形的梯度颜色

发布于 2025-01-25 01:35:20 字数 2681 浏览 5 评论 0原文

我想使用plotly

“

具体来说,我希望颜色尺度从绿色到渐变变化。我还想要一个指示值范围的侧色条。 x轴的值范围从01。较小值的条形接合到绿色。具有啤酒值的条逐渐变为红色。

下面是我当前的代码

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

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] 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2025-02-01 01:35:20

您没有将颜色分配给变量。此外,如果您使用colorscale,我敢肯定它需要嵌套在标记中。

此代码对colorbar有很多标注,因此它非常类似于您试图模拟的绘图。此外,我使用了set.seed(),因此我将能够获得可重复的结果。

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"))

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文