3D density plot in R with Plotly

Posted in General, R by Julyan Arbel on 30 June 2016


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.

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

Capture d’écran 2016-06-30 à 12.15.57


6 Responses

Subscribe to comments with RSS.

  1. mirriam ndunge said, on 1 July 2016 at 16:24

    Great information.

  2. Tiffany said, on 24 August 2016 at 17:51

    This 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?

  3. Julyan Arbel said, on 25 August 2016 at 22:23

    Hi 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.

  4. Daan said, on 8 June 2017 at 13:24

    Great 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.

  5. Julyan Arbel said, on 11 September 2017 at 15:03

    Hi Daan, I’ve fixed the data link.

  6. gurcanyavuz said, on 12 January 2018 at 19:53

    Very 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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: