Plotly R 设置连续颜色图例的标题

Plotly R setting the title of a continuous color legend

我正在尝试使用 Plotly 和 R 绘制 3D 散点图。除了 x、y 和 z,我还想根据第四个变量设置每个点的颜色。
我设法正确设置了绘图(name = ~res 的使用是在悬停时显示 res 的值),但我无法更改颜色条的名称。
这是我所做的模拟代码:

library(tidyverse)
library(plotly)

a = seq(1,10,1)
b = seq(100,1000,100)
c = seq(1,4.9,0.4)

data = tibble(a,b,c)
data <- data %>% mutate(res = a+b+c)

layout_details <- list(xaxis = list(title = 'a [-]'),
                       yaxis = list(title = 'b [-]'),
                       zaxis = list(title = 'c [-]'),
                       coloraxis=list(colorbar=list(title=list(text='Here are the results'))))

p = plot_ly(data, x = ~a, y = ~b, z = ~c, color = ~res, type = 'scatter3d', 
            mode = 'markers', name = ~res, showlegend = FALSE, scene = 'scene1')
p <- p %>% layout(scene1 = layout_details)
p

我注意到有人问了一个非常相似的问题 (R plotly to legend title value ignored for continuous color scatter plot),但没有任何答案。

有人知道怎么解决吗?
谢谢

您可以在 marker 参数中定义您的 colorbar

name 参数干扰了颜色条,因此我将 resname 参数移至 hovertemplatecustomdata

代码

p = plot_ly(data, x = ~a, y = ~b, z = ~c,
            name = "",
            scene = 'scene1',
            type = 'scatter3d', 
            mode = 'markers',
            customdata = as.list(data$res),
            hovertemplate = paste('x: %{x}',
                                  'y: %{y}',
                                  'z: %{z}',
                                  'name: %{customdata}',
                                  sep = "\n"),
            marker = list(color = ~res, 
                          colorbar = list(title = "Here are the results"),
                          colorscale='Viridis',
                          showscale = TRUE)) 

p <- p %>% layout(scene1 = layout_details)

p

情节