gpt4 book ai didi

r - 在 R Plotly 中命名 3D 曲面图的图例

转载 作者:行者123 更新时间:2023-12-04 00:32:24 26 4
gpt4 key购买 nike

我想知道是否有一种方法可以命名由 plotly 为同一绘图上的多个表面显示的图例,相当于 trace 方法:

在 plotly 上使用多表面示例:

https://plot.ly/r/3d-surface-plots/

library(plotly)
z <- c(
c(8.83,8.89,8.81,8.87,8.9,8.87),
c(8.89,8.94,8.85,8.94,8.96,8.92),
c(8.84,8.9,8.82,8.92,8.93,8.91),
c(8.79,8.85,8.79,8.9,8.94,8.92),
c(8.79,8.88,8.81,8.9,8.95,8.92),
c(8.8,8.82,8.78,8.91,8.94,8.92),
c(8.75,8.78,8.77,8.91,8.95,8.92),
c(8.8,8.8,8.77,8.91,8.95,8.94),
c(8.74,8.81,8.76,8.93,8.98,8.99),
c(8.89,8.99,8.92,9.1,9.13,9.11),
c(8.97,8.97,8.91,9.09,9.11,9.11),
c(9.04,9.08,9.05,9.25,9.28,9.27),
c(9,9.01,9,9.2,9.23,9.2),
c(8.99,8.99,8.98,9.18,9.2,9.19),
c(8.93,8.97,8.97,9.18,9.2,9.18)
)
dim(z) <- c(15,6)
z2 <- z + 1
z3 <- z - 1

p <- plot_ly(showscale = FALSE) %>%
add_surface(z = ~z) %>%
add_surface(z = ~z2, opacity = 0.98) %>%
add_surface(z = ~z3, opacity = 0.98)

If I change the ploty code to below:


p <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z, name="Example 1") %>%
add_surface(z = ~z2, opacity = 0.98,name="Example 2") %>%
add_surface(z = ~z3, opacity = 0.98,name="Example 3")

根据下面显示的跟踪示例,名称未显示在图例上方,正如人们所期望的那样:

https://plot.ly/r/legend/#legend-names
library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines',
legendgroup = 'group1', name = 'Zone 1 - Tree 1') %>%
add_trace(y = ~Tree2, legendgroup = 'group2', name = 'Zone 2 - Tree 1') %>%
add_trace(y = ~Tree3, legendgroup = 'group1', name = 'Zone 1 - Tree 2') %>%
add_trace(y = ~Tree4, legendgroup = 'group2', name = 'Zone 2 - Tree 2') %>%
add_trace(y = ~Tree5, legendgroup = 'group1', name = 'Zone 1 - Tree 3')

有没有办法获得具有多个表面的图例标签的等效结果?

最佳答案

你在找这个吗:

p <- plot_ly() %>%
add_surface(z = ~z, colorbar=list(title='Example 1')) %>%
add_surface(z = ~z2, opacity = 0.98, colorbar=list(title='Example 2')) %>%
add_surface(z = ~z3, opacity = 0.98, colorbar=list(title='Example 3'))

关于r - 在 R Plotly 中命名 3D 曲面图的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49108645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com