3d surface plots with RStudio and Plotly

Recently you may have seen how to build a 3d surface plot with Plotly and IPython notebook. Here you can learn the basics of creating a 3d surface plot with Plotly in RStudio.

Just add the Plotly library into your RStudio environment then add a Plotly username and key:

To create a surface plot, use two vectors: x and y of length m and n, and a matrix: z of size m*n. In this example, x and y both consist of 100 points ranging from -5 to 4.9.

*note this results in the same dimensions (1 column X 100 rows) as specifying:

The size of x is 1 column with 100 rows. In order to multiply x * y to create matrix z with 100 columns and 100 rows, y should be 100 columns with 1 row.

To multiply the vertical and horizontal vectors to create matrix z in RStudio, the basic syntax is z = x %*% y. In this example, a function is applied to z to create waves. Below, dimensions x, y, and z are defined. y used here is different than y1 used above because y should be the default, 1 column vector, not 1 row x 100 columns. Type is defined as “surface”.

Finally, specify layout information and filename:

The result will be similar to the interactive 3d plot displayed below:

Creating dashboards or visualizations at your company? Consider Plotly Enterprise for modern intracompany graph and data sharing.