[英]Adding colorscale to plotly
我正在嘗試 plot 3d 頻率 map,效果很好。 稍后,我想為 Z 軸創建色標。
我試過這個:
intensity = seq(0,30, length=31)
colorscale = list(
c(0, 'rgb(0, 255, 0)'),
c(15, 'rgb(247,255,0)'),
c(30, 'rgb(255, 0, 0)')
)
p<-plot_ly(data = d_long, x = ~i_DiaAlerta, y = ~variable, z = ~value, type="mesh3d",
intensity = intensity,
colorscale = colorscale,
showscale = TRUE)
p
它產生這個 plot: Plot
色標錯誤,plot 始終是白色 我的 d_long 圖如下所示:
頭(d_long)
i_DiaAlerta variable value
1 188 1 4
2 189 1 5
3 190 1 1
4 191 1 3
5 192 1 0
6 193 1 0
一個可行的替代方案,使用色標,它需要一個與 colors 向量長度相等的閾值向量(0 到 1)。
library(plotly)
d_long <- data.frame(
i_DiaAlerta = runif(30, min = 0, max = 30),
variable = runif(30, min = 0, max = 30),
value = runif(30, min = 0, max = 30)
)
thresholds_colors = seq(0, 1, length = 3)
colors = c('rgb(0, 255, 0)', 'rgb(247,255,0)', 'rgb(255, 0, 0)')
intensity = seq(0,30, length=31)
p <- plot_ly(data = d_long, x = ~i_DiaAlerta, y = ~variable, z = ~value, type = "mesh3d",
intensity = intensity,
colorscale = list(thresholds_colors, colors),
showscale = TRUE)
p
您需要傳遞colors
參數:
d_long <- data.frame(
i_DiaAlerta = runif(30, min = 0, max = 30),
variable = runif(30, min = 0, max = 30),
value = runif(30, min = 0, max = 30)
)
p <- plot_ly(data = d_long, x = ~i_DiaAlerta, y = ~variable, z = ~value, type="mesh3d",
intensity = ~value,
colors = colorRamp(c(
rgb(0, 255, 0, max = 255),
rgb(247,255,0, max = 255),
rgb(255, 0, 0, max = 255)
)),
showscale = TRUE)
p
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.