How to do it...

Here is how we design a 3 dimensional plot with plotly:

  1. To begin with, have your data created using MASS::kde2d()
> cars_d <- MASS::kde2d(cars$speed, cars$dist, n =50)
  1. Call plot_ly() and use add_surface() to create a 3D surface:
> library(plotly)> plot_ly(x = cars_d$x,           y = cars_d$y,           z = cars_d$z) %>%    add_surface()

A snapshot from the original output can be seen at the following image (Figure 8.12):

Figure 8.12 - plotly 3D surface

Let's see how the cards are playing.

Get R Data Visualization Recipes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.