Not able to create 3D subplots in R

I am not able to get all the plots in one picture with below code. This same code worked fine with type=“box” and “histogram”.
The code below only generates one plot, but if you then check “plots” variables it does have 3 plots.

I found the plotly with Python can do this but there is no R page for this. Check it out here: https://plot.ly/python/3d-subplots/

Please help.

df <- read.csv(‘https://raw.githubusercontent.com/plotly/datasets/master/3d-line1.csv’)
lst <- levels(factor(df$color))
plots <- list()
for(i in 1:nlevels(factor(df$color))){
subdf <- dplyr::filter(df, color == lst[i])
dens = akima::interp(subdf$x, subdf$y, subdf$z, duplicate = “mean”)
p <- plot_ly(x = dens$x, y = dens$y, z = dens$z, colors = c(“blue”,“red”), type = “surface”)
plots[[i]] <- p
}
subplot(plots, nrows = 2)

Hey @tarunparmar,

Like in the python example, the scene can be set in R:

p1 <- plot_ly(z = ~volcano, scene='scene1') %>% add_surface()
p2 <- plot_ly(z = ~volcano, scene='scene2') %>% add_surface()
subplot(p1, p2)

@bcd
Hi Branden

Thanks again for your reply.
I tried your code but still not able to see the output as yours. Not sure if its my system or some kind of package dependency issue.

Here’s my session info:

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] plotly_4.5.6  ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8       magrittr_1.5      munsell_0.4.3     colorspace_1.3-2 
 [5] viridisLite_0.1.3 lattice_0.20-34   R6_2.2.0          httr_1.2.1       
 [9] plyr_1.8.4        dplyr_0.5.0       tools_3.3.1       grid_3.3.1       
[13] gtable_0.2.0      DBI_0.5-1         htmltools_0.3.5   yaml_2.1.14      
[17] lazyeval_0.2.0    assertthat_0.1    digest_0.6.11     tibble_1.2       
[21] akima_0.6-2       purrr_0.2.2       tidyr_0.6.0       base64enc_0.1-3  
[25] htmlwidgets_0.8   sp_1.2-4          scales_0.4.1      jsonlite_1.2

I’m using plotly_4.5.6.9000

That did fix the problem. But weird with everything matched to exact version, it works fine on my Mac but doesnt work on my work Windows laptop.

Also on the mac, it seem to only plot all the subplots in one row even with nrows = 2.

p1 <- plot_ly(z = ~volcano, scene='scene1') %>% add_surface()
p2 <- plot_ly(z = ~volcano, scene='scene2') %>% add_surface()
p3 <- plot_ly(z = ~volcano, scene='scene3') %>% add_surface()
p4 <- plot_ly(z = ~volcano, scene='scene4') %>% add_surface()
subplot(p1, p2, p3, p4, nrows = 2)

1 Like

you can specify the domain in the subplot layout to control the position of each plot:

p1 <- plot_ly(z = ~volcano, scene='scene1') %>%
  add_surface()

p2 <- plot_ly(z = ~volcano, scene='scene2') %>%
  add_surface()

subplot(p1, p2) %>% 
  layout(scene = list(domain=list(x=c(0.1,0.5),y=c(0.1,0.5))),
         scene2 = list(domain=list(x=c(0.5,0.9),y=c(0.5,0.9))))

I want to program it such that the plots get arranged sequentially for the number of levels in a dataset.

Just like subplot() will do if I have 20 plots with nrows = 5

This bug still remains on windows. Cant subplot 3d. It works only running the volcano example. On any of my plots, subplots igores the list and just plot the last one on the argument. Already combined one of the volgano figs and one i did and works, what it is odd. It is like only the example can be subplotted.

Excellent, thanks! Would it be possible to show only one legend?
I have tried with fig <- p1 %>% layout(showlegend = FALSE), but it doesn’t work.