Iām trying to display 13 different 3D plots in a single figure using subplot, but all the plots are overlapping one another.
My code:
# custom grid style
axx <- list(
gridcolor='rgb(255, 255, 255)',
zerolinecolor='rgb(255, 255, 255)',
showbackground=TRUE,
backgroundcolor='rgb(230, 230,230)'
)
create_plotly_fig <- function(data, cat, plotnum) {
cat_data = data %>% filter(type == cat)
cat_probs = matrix(cat_data$prob, nrow = 50, ncol = 50)
fig = plot_ly(x = seq(mintemp, maxtemp, len=50), y = seq(minco2, maxco2, len=50), z = ~cat_probs, scene = paste("scene", plotnum, sep="")) %>% add_surface(showscale = FALSE)
return(fig)
}
create_scene <- function(cat, rid, cid, xcoord, ycoord, scene) {
return(list(domain=list(x=xcoord, y=ycoord, row=rid, column=cid),
xaxis=c(axx, id=scene),
yaxis=c(axx, id=scene),
zaxis=c(axx, title=paste(cat, "Probability"), id=scene),
aspectmode='cube'))
}
fig1 <- create_plotly_fig(z, "Animal accident", 1)
fig2 <- create_plotly_fig(z, "Drought", 2)
fig3 <- create_plotly_fig(z, "Earthquake", 3)
fig4 <- create_plotly_fig(z, "Epidemic", 4)
fig5 <- create_plotly_fig(z, "Extreme temperature ", 5)
fig6 <- create_plotly_fig(z, "Flood", 6)
fig7 <- create_plotly_fig(z, "Impact", 7)
fig8 <- create_plotly_fig(z, "Insect infestation", 8)
fig9 <- create_plotly_fig(z, "Landslide", 9)
fig10 <- create_plotly_fig(z, "Mass movement (dry)", 10)
fig11 <- create_plotly_fig(z, "Storm", 11)
fig12 <- create_plotly_fig(z, "Volcanic activity", 12)
fig13 <- create_plotly_fig(z, "Wildfire", 13)
# subplot and define scene
fig <- subplot(fig1, fig2, fig3, fig4, fig5, fig6, fig7, fig8, fig9, fig10, fig11, fig12, fig13, nrows = 4, heights = c(10,10,10,10), widths = c(10,10,10,10))
fig <- fig %>% layout(title = "Regressing Disasters on Temp and CO2", height = 500, width = 500, margin = 10,
scene = create_scene("Animal accident", 0, 0, c(0,0.5), c(1.5,2), 1),
scene2 = create_scene("Drought", 0, 1, c(0.5,1), c(1.5,2), 2),
scene3 = create_scene("Earthquake", 0, 2, c(1,1.5), c(1.5,2), 3),
scene4 = create_scene("Epidemic", 0, 3, c(1.5,2), c(1.5,2), 4),
scene5 = create_scene("Extreme temperature", 1, 0, c(0,0.5), c(1,1.5), 5),
scene6 = create_scene("Flood", 1, 1, c(0.5,1), c(1,1.5), 6),
scene7 = create_scene("Impact", 1, 2, c(1,1.5), c(1,1.5), 7),
scene8 = create_scene("Insect infestation", 1, 3, c(1.5,2), c(1,1.5), 8),
scene9 = create_scene("Landslide", 2, 0, c(0,0.5), c(0.5,1), 9),
scene10 = create_scene("Mass movement (dry)", 2, 1, c(0.5,1), c(0.5,1), 10),
scene11 = create_scene("Storm", 2, 2, c(1,1.5), c(0.5,1), 11),
scene12 = create_scene("Volcanic activity", 2, 3, c(1.5,2), c(0.5,1), 12),
scene13 = create_scene("Wildfire", 3, 0, c(0.75,1.25), c(0,0.5), 13)
)
fig
And my results:
Please let me know if any additional information is needed.
Thanks,
Sam