[R] plotting planes and lines in wireframe()

Deepayan Sarkar deepayan at stat.wisc.edu
Sat May 8 01:05:03 CEST 2004


On Friday 07 May 2004 16:07, wwsprague at ucdavis.edu wrote:
> Hi R-helpers
>
> I would like to plot some planes which are perpendicular to the x-y
> plane, such as x=y.  Is there a way to do this in wireframe?  I
> realize that I am not plotting a function of x, y since there are
> infinite number of z's that satisfy the above relation....  Hmm...

Well, from R 1.9.0 onwards wireframe can draw arbitrary 3-D parametric 
surfaces (spheres and such) with a formula of the form z ~ x * y where 
x, y and z are all matrices. A plane is a very trivial application of 
this, e.g.:

x <- matrix(rep(1:10, 10), 10)
y <- x
z <- matrix(rep(1:10, each = 10), 10)
wireframe(z ~ x * y)

Of course, you are probably not interested in only this plane, in which 
case things may not be as easy, since (1) formally there's no way to 
specify multiple surfaces with this usage and (2) wireframe doesn't do 
hidden surface removal, so intersecting surfaces are messed up. But in 
many cases you can probably make do with a fine mesh and some 
innovative use of NA's. e.g. (continuing the earlier example):

xx <- cbind(x, NA, x)
yy <- cbind(y+50, NA, y^2)
zz <- cbind(z, NA, z)
wireframe(zz ~ xx * yy)

> Somewhat related, I would also like to plot a line in 3 d space...

That's also easy, e.g. cloud(1:3 ~ 3:1 * 1:3) (there seems to be bug 
with just 2 points, I'll fix that in the next release). To do this in a 
wireframe plot you would probably use the panel function 
panel.3dscatter. What's not easy, however, when you are combining 
planes and lines, is to make sure that the appropriate parts of the 
line are hidden.

> Finally, if you are feeling really brave, I am interested in doing
> all of this based on sums generating vectors (I know there is a
> linear algebra term for this) -- ie generate the plane based on
> c_1*v_1 + c_2*v_2 for c_1 and c_2 in reals.

You mean the span of v_1 and v_2 ? Very easy with the approach outlined 
above. Visualize a rectangular grid on the c_1-c_2 plane determining 
the ranges of c_1 and c_2. Let's say the ranges are c1 and c2, e.g., c1 
= c2 = seq(0, 1, length = 11). Next, evaluate the values of c1 and c2 
on this grid as matrices:

tmp = expand.grid(c1 = c1, c2 = c2)
mc1 = matrix(tmp$c1, length(c1))
mc2 = matrix(tmp$c2, length(c1))

Next evaluate the corresponding points on your plane:

x = v1[1] * mc1 + v2[1] * mc2
y = v1[2] * mc1 + v2[2] * mc2
z = v1[3] * mc1 + v2[3] * mc2

and plot them

wireframe(z ~ x * y)

If you don't like the distortion, you could use a wide range of c1 and 
c2 and then use appropriate x/y/zlim.

Deepayan




More information about the R-help mailing list