Rafael Gonçalves Rafael Gonçalves - 1 month ago 24
R Question

Plot multiple surfaces

I'm trying and plot multiple surfaces on the same chart with Plotly package but I just can't get it done. A few days ago I could do this with no problem using the same code, but now it seems that after an update on Plotly it's not possible anymore. Here is an example of what I'm trying to do:

#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)


When I run the code above, I get the following output:

Error in p$x$data[[idx]]$marker :
$ operator is invalid for atomic vectors


If I assign the Plotly object to some variable instead of using the '%>%' operator, like this:

#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


... then I get the same output as before.

If I plot the volcano and just one of the rectangles, it works just fine. Furthermore, I've tried to plot different surfaces than the rectangles and, still, I get the same error.

Here is some information that might help:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
[1] LC_CTYPE=pt_BR.UTF-8 LC_NUMERIC=C LC_TIME=pt_BR.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=pt_BR.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=pt_BR.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base

other attached packages:
[1] plotly_4.5.2 ggplot2_2.1.0 shiny_0.14.1 doMC_1.3.4
[5] iterators_1.0.8 quantstrat_0.9.1739 foreach_1.4.3 blotter_0.9.1741
[9] PerformanceAnalytics_1.4.4000 FinancialInstrument_1.2.0 quantmod_0.4-6 TTR_0.23-1
[13] xts_0.9-7 zoo_1.7-13

loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 compiler_3.3.1 plyr_1.8.4 base64enc_0.1-3 tools_3.3.1 digest_0.6.10 viridisLite_0.1.3
[8] jsonlite_1.1 tibble_1.2 gtable_0.2.0 lattice_0.20-34 DBI_0.5-1 yaml_2.1.13 dplyr_0.5.0
[15] httr_1.2.1 htmlwidgets_0.7 grid_3.3.1 R6_2.1.3 purrr_0.2.2 tidyr_0.6.0 magrittr_1.5
[22] scales_0.4.0 codetools_0.2-14 htmltools_0.3.5 assertthat_0.1 mime_0.5 xtable_1.8-2 colorspace_1.2-6
[29] httpuv_1.3.3 lazyeval_0.2.0 munsell_0.4.3


Any thoughts about that issue?

Answer

Solved. Re-wrote code to:

#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))

Works like a charm. Did it based on this multiple surfaces plot example.