R 绘制多个曲面

R 绘制多个曲面,r,plotly,R,Plotly,我正在尝试用Plotly软件包在同一张图表上绘制多个曲面,但我就是做不到。几天前,我可以使用相同的代码完成这项工作,但现在看来,在Plotly上进行更新之后,这项工作就不可能了。下面是我尝试做的一个例子: #Volcano surface plot_ly(z = volcano, type = 'surface') %>% #First rectangle add_trace(x = c(10, 60), y = c(10, 50),

我正在尝试用Plotly软件包在同一张图表上绘制多个曲面,但我就是做不到。几天前,我可以使用相同的代码完成这项工作,但现在看来,在Plotly上进行更新之后,这项工作就不可能了。下面是我尝试做的一个例子:

#Volcano surface
plot_ly(z = volcano,
        type = 'surface') %>%

#First rectangle
add_trace(x = c(10, 60),
          y = c(10, 50),
          z = matrix(160, nrow = 2, ncol = 2),
          type = 'surface', showscale = FALSE) %>%

#Second rectangle
add_trace(x = c(10, 60),
          y = c(10, 50),
          z = matrix(180, nrow = 2, ncol = 2),
          type = 'surface',
          showscale = FALSE)
当我运行上述代码时,我得到以下输出:

Error in p$x$data[[idx]]$marker : 
  $ operator is invalid for atomic vectors
如果我将Plotly对象指定给某个变量,而不是使用“%>%”运算符,如下所示:

#Volcano surface
p <- plot_ly(z = volcano,
             type = 'surface')

#First rectangle
p <- add_trace(p,
               x = c(10, 60),
               y = c(10, 50),
               z = matrix(160, nrow = 2, ncol = 2),
               type = 'surface', showscale = FALSE)

#Second rectangle
p <- add_trace(p,
               x = c(10, 60),
               y = c(10, 50),
               z = matrix(180, nrow = 2, ncol = 2),
               type = 'surface',
               showscale = FALSE)

#Plot object
p

对那个问题有什么想法吗?

解决了。将代码重新写入:

#Create Plotly object
plot_ly(showscale = FALSE) %>%

#Volcano surface    
add_surface(z = volcano) %>%

#First rectangle
add_surface(x = c(10, 60),
            y = c(10, 50),
            z = matrix(160, nrow = 2, ncol = 2)) %>%

#Second rectangle
add_surface(x = c(10, 60),
            y = c(10, 50),
            z = matrix(180, nrow = 2, ncol = 2))
工作起来很有魅力。它是基于多个曲面绘制的示例

#Create Plotly object
plot_ly(showscale = FALSE) %>%

#Volcano surface    
add_surface(z = volcano) %>%

#First rectangle
add_surface(x = c(10, 60),
            y = c(10, 50),
            z = matrix(160, nrow = 2, ncol = 2)) %>%

#Second rectangle
add_surface(x = c(10, 60),
            y = c(10, 50),
            z = matrix(180, nrow = 2, ncol = 2))