R: Fun with surf3D function

February 19, 2014

(This article was first published on Analysis with Programming, and kindly contributed to R-bloggers)

There is one package that I’ve been longing. A package that will give me the power to manipulate and do any 3D stuffs in R. I tried persp and wireframe, but I find them difficult to use especially on complicated mathematical functions, like doing parametric plots. And I am just frustrated about that, since I envy the 3D graphics of Wolfram Mathematica a lot, which I exploited for about a year, before I embrace R. However, that has come to an end after Joseph Rickert introduced the plot3D (authored by Karline Soetaert) package in his post. And for the moment, we will be playing with the surf3D function. Here is the first one, the Mollusc Shell surface plot:

With parametric equations:
x(u,v)&=&\left(1.16^v\right)(1 + \cos(u))\cos(v);\nonumber\\
y(u,v)&=&\left(-1.16^v\right)(1 + \cos(u))\sin(v);\nonumber\\
z(u,v)&=&\left(-2\times 1.16^v\right)(1 + \sin(u));\nonumber
$$where $u\in[0, 2\pi],\,v\in[-15, 6]$. By the way, I am not sure if that’s the general equations of the Mollusc shell, I can’t find one on the web. In case you know it, please inform me by commenting below. The above equations was based on the Mathematica Documentation, with code

Now with extra animation, here is the R equivalent:

Next is the “figure 8” immersion (Klein bagel) of the Klein bottle:
z(u,v) &=&\sin\left(\displaystyle\frac{1}{2}u\right)\sin(v) + \cos\left(\displaystyle\frac{1}{2}u\right)\sin(2v).\nonumber
$$For $u\in [0,2\pi),\,v\in[0,2\pi),$ and $a>2$.

Which in R,

Now let’s investigate the script of this static, transparent Mollusc Shell:

  1. Call the package, that’s obviously the first line;
  2. Line 3 to 7 basically assigns the ranges of $u$ and $v$ variables;
  3. Line 10 to 12 are the parametric equations of the shape;
  4. Line 15 adjusts the margin of the plot on all sides in inches; and finally
  5. Line 16 to 20 generates the plot.

Mathematics is cool. I will leave you with this quote from my favourite TV show, Numb3rs:

Math is nature’s language, it’s method of communicating directly with us. So everything is Numbers. ~ Charlie Eppes from Numb3rs – Sabotage, Season I.


  1. Klein Bottle – Wikipedia. Retrieved February 18, 2014.
  2. Weisstein, Eric W. “Klein Bottle.” From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.com/KleinBottle.html

To leave a comment for the author, please follow the link and comment on their blog: Analysis with Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)