[R] Simple 3D Plot Question

Duncan Murdoch murdoch.duncan at gmail.com
Tue Jun 15 14:15:28 CEST 2010


Nick Torenvliet wrote:
> Hi all,
>
> I've got a simple 3D plot as follows...
>
>   
>> xx <- seq(-20,20,.5)
>> yy <- seq(-20,20,.5)
>> zFunc <- function(x,y){3*x^2*y}
>> z <- outer(xx,yy,zFunc)
>> persp(xx,yy,z,theta=30,phi=30,ticktype="detailed")
>>     
>
> Just beautiful!
>
> My question is how do I constrain the plot to only display x^2 <= y <= 1?

If you set z values to NA, the surface mesh won't be plotted.  Those 
restrictions you give are a pretty small part of the range of xx and yy 
though, so you'll want to restrict them, e.g.

xx <- seq(-1,1,len=20)
yy <- seq(0,1,len=20)
zFunc <- function(x,y){ifelse(x^2 < y & y < 1, 3*x^2*y, NA)}
z <- outer(xx,yy,zFunc)
persp(xx,yy,z,theta=30,phi=30,ticktype="detailed")

Duncan Murdoch



More information about the R-help mailing list