# [R] persp plot + plotting grid lines

Duncan Murdoch murdoch at stats.uwo.ca
Sun Mar 15 16:34:43 CET 2009

```On 15/03/2009 10:07 AM, Kingsford Jones wrote:
> Building on Duncan's code, here's an approximation to the Matlab
> 'peaks' plot referred to by Pedro:
>
> peaks <-  function(x, y) { 3 * (1-x)^2 * exp(-(x^2)-(y+1)^2) -
>   10 * (x/5-x^3-y^5) * exp(-x^2-y^2) - 1/3*exp(-(x+1)^2-y^2)}
>
> x <- y <- seq(-3,3,.1)
> z <- outer(x,y, peaks)
> z2 <- 10 * round(c(z) + abs(min(z)) + 1)
> jet.colors = colorRampPalette(c("#00007F", "blue", "#007FFF",
>     "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
> color <- jet.colors(160)[z2]
>
> library(rgl)
> persp3d(x,y,z, color=color, smooth=FALSE)
> surface3d(x,y,z+0.001, front="lines", back="culled")

Using the textured grid as in my second option looks a bit better here.

The other big difference between rgl output and the Matlab output is
that Matlab's looks flat, whereas rgl does some fairly complicated
lighting calculations.  But if you don't want the shiny plastic look,
you can partially turn it off by including the following:

persp3d(x,y,z, color=color, smooth=FALSE, specular="black")

Or turn it off completely with

persp3d(x,y,z, color=color, smooth=FALSE, lit = FALSE)

>
>
>
> Kingsford Jones
>
>
> On Sat, Mar 14, 2009 at 3:51 PM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 14/03/2009 12:02 PM, Pedro Mardones wrote:
>>> Dear all;
>>> Does anyone know how to add grid lines to a persp plot? I've tried
>>> using lines(trans3d..) but the lines of course are superimposed into
>>> the actual 3d surface and what I need is something like the plot shown
>>> http://thermal.gg.utah.edu/tutorials/matlab/matlab_tutorial.html
>>> I'll appreciate any ideas
>> I just posted a couple of demos of this using the rgl function persp3d, in
>> the thread "Re: [R] can I draw 3D plot like this using R?".
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help