# [R] How to plot Ellipsoid like function

Duncan Murdoch murdoch.duncan at gmail.com
Tue Dec 14 15:13:46 CET 2010

```On 14/12/2010 8:45 AM, Uwe Wolfram wrote:
> Dear R-Users,
>
> I am trying to plot an ellipsoid like function that represents some
> physical threshold in its eigenvalue space. I am facing a few problems
> generating a figure I need for my thesis. A small example looks as
> follwos where the two contour3d plots do NOT overlay as desired so you
> may try plotting the surfaces one by one to see what I mean.
>
> ====================================================================
> # begin example
>
> require(rgl)
> require(misc3d)
> require(MASS);
>
> f<- function(x, y, z){
>      chi0=-0.6603368
>      eps0=0.006590395
>      xi0=0.01117194
>      (x^2 + y^2 + z^2 - chi0*(x*y + x*z + y*z))/eps0^2 + (x + y + z)/xi0
> }
> ff<- function(x, y, z)x + y + z
>
> open3d()
> clear3d("all")
> bg3d(color="#887777")
> light3d()
>
> x<- seq(-.02,.02,len=20)
>
> # plot ellipsoid
> contour3d(f,1,x,x,x,color="#FF2222",alpha=0.5)
>
> # plot plane
> contour3d(ff,1,x,x,x,color="#FFCCCC",alpha=0.5)
>
> # plot data points
> spheres3d(c(-0.009379952, 0.007899338), c(-0.00879318, 0.00700924),
>
> # plot hydrostatic pressure line
> lines3d(c(-0.012, 0.012), c(-0.012, 0.012), c(-0.012, 0.012),
> col="#A8A8A8", lwd=4)
>
> # end example
> ====================================================================
>
> I have three questions regarding this problem and I hope you could help
> me.
>
>       1. How can I overlay the plan plotted using contours3d(ff, ...) and
>          the ellipsoid plotted with contours3d(f, ...)

Use "add=TRUE" in the second call to contour3d.  However, the level you
chose (i.e. 1) never occurs in the range of your x,y,z data, so you
won't see anything unless you change it.

>       2. Instead of using spheres3d(...) I would love to use plot3d to
>          obtain proper x, y and z coordinate axes. Is there a possibility
>          to overly the contour3d() and line3d() commands with pot3d?
>          Otherwise is there a possibility to plot proper coordinate axes
>          with tics and such as usual R plots?

Use decorate3d() to add the axes etc, or axes3d() for just axes.
>       3. How can I save the scene to an image? pdf(...) ... dev.off()
>          seems not to work on my machine. I am using Ubuntu on a 32 Bit
>          Laptop.

That's harder, but you can try rgl.snapshot() (for a bitmap copy in PNG
format) or rgl.postscript() (for vector graphics in
ps, eps, tex, pdf, svg, or pgf, but with incomplete rendering of some
scenes).

Duncan Murdoch

> Thanks a  million for your help!
>
> Uwe
>
>
> Am Montag, den 13.12.2010, 10:20 -0500 schrieb Duncan Murdoch:
> >  On 13/12/2010 10:13 AM, Uwe Wolfram wrote:
> >  >
> >  >
> >  >  I am currently trying to fit a tensorial function in its principal
> >  >  coorinate system. The function is given by:
> >  >
> >  >  1~(x1^2 + x2^2 + x3^2 - chi0*(x1*x2 + x1*x3 + x2*x3))/eps0^2 + (x1 + x2
> >  >  + x3)/xi0
> >  >
> >  >  Where eps0 = 0.0066, chi0 = -0.66 and xi0 = 0.011 are obtained from
> >  >  experimental data using nls().I am able to plot the experimental points
> >  >  that delivered the parameters of the function. For my thesis, however, I
> >  >  need to overlay the fitted surface. So far I am using the following code
> >  >  which wonderfully plots the experimental points in 3D:
> >  >
> >  >  ===================================================================
> >  >  # from demo(bivar)
> >  >  require(rgl)
> >  >  require(misc3d)
> >  >  require(MASS);
> >  >
> >  >  # New window
> >  >  open3d()
> >  >  # clear scene:
> >  >  clear3d("all")
> >  >  # setup env. That is, background, light and so on:
> >  >  bg3d(color="#887777")
> >  >  light3d()
> >  >  # spheres at points in principal strain space
> >  >  # draws points alternatively
> >  >  plot3d(e1,e2,e3, col="#CCCCFF")
> >  >  ===================================================================
> >  >
> >  >  According to the examples on http://rgl.neoscientists.org/gallery.shtml
> >  >  I tried to overlay the point plot using surface3d. However, these were
> >  >  only functions of type y ~f(x1, x2). I think that the surface could be
> >  >  plotted if I could provide the gridpoints correctly. Using
> >  >  xyz.coords(1~(x1^2 + x2^2 + x3^2 - chi0*(x1*x2 + x1*x3 + x2*x3))/eps0^2
> >  >  + (x1 + x2 + x3)/xi0, y = NULL, z = NULL) did unfortunately not solve
> >  >  the problem.
> >  >
> >  >  Is there any function that can generate the surface for the given
> >  >  function such as ContourPlot3D in Mathematica.
> >
> >  See ?misc3d::contour3d
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help