True three-dimensional plots can be drawn using plotly
. There are a wide range of types available, from 3D scatter plots, to 3D lines, 3D surfaces, and 3D meshes. There is a great thing about 3D plots made with plotly
--the user is able to drag the illustration, see it from different angles, and zoom in and out.
Make sure that the MASS
package is already downloaded and installed:
> if( !require(MASS)){ install.packages('MASS')}
If it's missing and internet connection is fine, above code will do the job for you.
Here is how we design a 3 dimensional plot with plotly
:
- To begin with, have your data created using
MASS::kde2d()
:
> cars_d <- MASS::kde2d(cars$speed, cars$dist, n =50)
- Call
plot_ly()
and useadd_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...