R plotly:条形图,条形图带有彩虹般的渐变颜色
R plotly: bar plot with rainbow like gradient color across bars
我想使用 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]
您没有将颜色分配给变量。此外,如果您使用 colorscale
,我很确定它需要嵌套在 marker
.
中
此代码对 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"))
我想使用 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]
您没有将颜色分配给变量。此外,如果您使用 colorscale
,我很确定它需要嵌套在 marker
.
此代码对 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"))