## 3D density plot in R with Plotly

In Bayesian nonparametrics, many models address the problem of *density regression*, including covariate dependent processes. These were settled by the pioneering works by [current ISBA president] MacEachern (1999) who introduced the general class of dependent Dirichlet processes. The literature on dependent processes was developed in numerous models, such as nonparametric regression, time series data, meta-analysis, to cite but a few, and applied to a wealth of fields such as, e.g., epidemiology, bioassay problems, genomics, finance. For references, see for instance the chapter by David Dunson in the Bayesian nonparametrics textbook (edited in 2010 by Nils Lid Hjort, Chris Holmes, Peter Müller and Stephen G. Walker). With Kerrie Mengersen and Judith Rousseau, we have proposed a dependent model in the same vein for modeling the influence of fuel spills on species diversity (arxiv).

Several densities can be plotted on the same 3D plot thanks to the Plotly R library, *“an interactive, browser-based charting library built on the open source JavaScript graphing library, plotly.js.”*

In our ecological example, the model provides a series of densities on the *Y* axis (in our case, posterior density of species diversity), indexed by some covariate *X* (a pollutant). See file density_plot.txt. The following Plotly R code provides a graph as below. For the interactive version, see the RPubs page here.

library(plotly) mydata = read.csv("density_plot.txt") df = as.data.frame(mydata) plot_ly(df, x = Y, y = X, z = Z, group = X, type = "scatter3d", mode = "lines")

mirriam ndungesaid, on 1 July 2016 at 16:24Great information.

Tiffanysaid, on 24 August 2016 at 17:51This is great! Unfortunately when I run your code I do not produce a plot. The data look fine and the library is loaded. Any thoughts?

Julyan Arbelsaid, on 25 August 2016 at 22:23Hi Tiffany, have you updated to the latest version of R? Do you use RStudio? You can try it on the RPubs link which works fine.

Daansaid, on 8 June 2017 at 13:24Great plot! I would like to make a similar plot with my own data. However, the density_plot.txt example file is no longer available. Could you please make it available! Thanks.

Julyan Arbelsaid, on 11 September 2017 at 15:03Hi Daan, I’ve fixed the data link.

gurcanyavuzsaid, on 12 January 2018 at 19:53Very nice plot. I did not get a plot at first. I added a tilde sign in front of x, y and z to get a graph.