3d Plots – R Code

In this script, we create a 3d plot in R using the plot3D package. In this example, a sphere is generated but multiple 3d shapes can be created by modifying the formulas for x,y,z.


# 3d plots

# clear environment and console
rm(list = ls())
cat("\014")

# install and load required packages
#install.packages("plot3D")

library("plot3D")

# initiate Variables
theta = seq(from = 0, to = 2 * pi, length.out = 500)
phi = seq(from = 0, to = pi, length.out = 500)
rho = 3 # random value

M = mesh(theta, phi)
alpha = M$x
beta = M$y

# plot sphere
surf3D(x = rho * cos(alpha) * sin(beta), y = rho * sin(alpha) * sin(beta), z = rho * cos(beta), colkey=FALSE, bty="b2", main="Sphere")

This is the output for 500 points generated for each variable:

Rplot

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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