[英]Plotly R setting the title of a continuous color legend
I'm trying to plot a 3D scatter using Plotly and R.我正在尝试使用 Plotly 和 ZE1E1D3D40573127E9EE2483 散布 plot 一个 3D 散点图。 Other than x, y and z I also would like to set the color of each point depending on a fourth variable.
除了 x、y 和 z,我还想根据第四个变量设置每个点的颜色。
I manage to set the plot correctly (the use of name = ~res
is to show the value of res
while hovering), but I am not able to change the name of the colorbar.我设法正确设置了 plot (使用
name = ~res
是在悬停时显示res
的值),但我无法更改颜色条的名称。
This is a mock code of what I've done:这是我所做的模拟代码:
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
I've noticed that a quite similar question was asked ( R plotly to legend title value ignored for continuous color scatter plot ), but without any answers.我注意到有人问了一个非常相似的问题( R plotly 到图例标题值忽略了连续颜色散布 plot ),但没有任何答案。
Does anyone know how to solve this?有谁知道如何解决这个问题?
Thanks谢谢
You can define your colorbar
inside the marker
argument.您可以在
marker
参数中定义colorbar
。
The name
argument is interfering with the colorbar therefore I moved res
from the name
argument to the hovertemplate
and the customdata
. name
参数干扰了颜色条,因此我将res
从name
参数移到了hovertemplate
和customdata
。
Code代码
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.