映射颜色时将多列传递给绘图中的自定义数据

问题描述 投票:0回答:0

如果我没有将任何值映射到

color
,则以下代码可以顺利运行:

library(tidyverse)
library(plotly)

set.seed(123)
d <- tibble(x = 1:10, 
            g = gl(2, 5), 
            y = sample(100, 10), 
            L = LETTERS[1:10], l = letters[1:10])

plot_ly(d) %>%
  add_bars(x = ~ x, y = ~ y, customdata = ~ map2(l, L, list),
           hovertemplate = "%{customdata[0]}:%{customdata[1]}")

但是,当我尝试为条形提供不同的颜色时,数据的(内部)形状发生变化,并且 R 抱怨形状不匹配:

plot_ly(d) %>%
  add_bars(x = ~ x, y = ~ y, color = ~ g, customdata = ~ map2(l, L, list),
           hovertemplate = "%{customdata[0]}:%{customdata[1]}")
Error in `recycle_columns()`:
! Tibble columns must have compatible sizes.
• Size 5: Columns `x`, `y`, `color`, `hovertemplate`, `.plotlyTraceIndex`, and
  2 more.
• Size 10: Column `customdata`.
ℹ Only values of size one are recycled.
Run `rlang::last_trace()` to see where the error occurred.

显然,我的

customdata
形状错误,但我不知道我需要如何传递它,以便plotly可以使用它?

r plotly
© www.soinside.com 2019 - 2024. All rights reserved.