簡體   English   中英

Plotly R 設置連續顏色圖例的標題

[英]Plotly R setting the title of a continuous color legend

我正在嘗試使用 Plotly 和 ZE1E1D3D40573127E9EE2483 散布 plot 一個 3D 散點圖。 除了 x、y 和 z,我還想根據第四個變量設置每個點的顏色。
我設法正確設置了 plot (使用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 到圖例標題值忽略了連續顏色散布 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

Plot 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM