[R] 3D to 2D projection

David Winsemius dwinsemius at comcast.net
Mon Sep 28 20:56:59 CEST 2009


On Sep 28, 2009, at 1:41 PM, Nair, Murlidharan T wrote:

> David,
> Have you used persp or trans3d before?

Yes, both.

persp requires an x-y grid of a particular sort ... from the help  
page ....

Arguments
x, y
locations of grid lines at which the values in z are measured. These  
must be in ascending order. By default, equally spaced values from 0  
to 1 are used. If x is a list, its components x$x and x$y are used for  
x and y, respectively.

> Here is a little piece of data that I am want to convert to 2d.

Perhaps you should define what you mean by "convert to 2d" in more  
expansive terms.

> I can plot (x,z) or (z,y). I know there is a better way to convert  
> it ....   ????what is "it"???

The use of indefinite pronouns is quite commonly a barrier to  
communication.

> ... to 2d. I did it long time back in my 3d geometry class.

I am not precisely clear what you are having problems with. I executed  
your code without any error reported. The s3d object looked like the 2- 
d projection of a single spiraled DNA-like shape.   Then I plotted the  
points you specified, again without error or modification of your code  
and now I "see": a) a second intertwined spiral curve, b) circles at  
the endpoints of the pseudo-3D segments, and c) a line that goes  
through the apparent center of the now pseudo-double-helix. Very pretty.

s3d is now as set of 4 functions that will project points or other  
objects into that virtual space:
 > str(s3d)
List of 4
  $ xyz.convert:function (x, y = NULL, z = NULL)
  $ points3d   :function (x, y = NULL, z = NULL, type = "p", ...)
  $ plane3d    :function (Intercept, x.coef = NULL, y.coef = NULL, lty  
= "dashed",
     lty.box = NULL, ...)
  $ box3d      :function (...)

You may have problems if the abstract object (a spiral)  does not  
conform to the requirements of the function you are considering  
(persp), since it may not have a single value at a particular  
combination of x and y. I suppose you could trick persp into plotting  
just the corners of a grid while assigning the output to a "res"  
object (as illustrated in the persp help page), and then using  
lines(trans3d(x,y,z,res ) ) and points(trans3d(x,y,z, res) ) on the  
x,y,z of your interest.

As Jim Holtman's tagline says: What problem are you trying to solve.?

-- 
David

>
> Thanks ../Murli
>
>
> library(scatterplot3d)
> dnaStr<-structure(list(x = c(-0.975688, -6.23153132820699,  
> -9.09624485603214,
> -8.63556544492323, -4.93169249022181, 0.543553938928959,  
> 5.64174931291628,
> 8.34431911056127, 7.41509534849297, 3.25793817052871,  
> -2.65307138974052,
> -8.1739402999332, -11.2739770817494, -11.0523961376876, -0.975688,
> 4.65283498179159, 8.34690510167613, 8.79226549387186,  
> 5.76337877843935,
> 0.36011024214207, -5.41074167495601, -9.54555893851015,  
> -10.4135928669112,
> -7.82046928259718, -2.87048496956275, 2.43182259657596,  
> 5.78000473462146,
> 5.86423147485841, 0, 0, -0.0747010058197876, -0.224103017459363,
> -0.373505029098938, -0.522907040738513, -0.672309052378088,  
> -0.895991523123606,
> -1.19395445297507, -1.49191738282653, -1.78988031267799,  
> -2.08784324252945,
> -2.45924790562486, -2.90409430196421), y = c(9.258795,  
> 6.91702748519505,
> 1.94818850352474, -3.96996047465861, -8.44783195266353,  
> -9.85357650887797,
> -7.72879278806027, -2.98312550512766, 2.29012749945630,  
> 6.14410676772742,
> 6.95007482866252, 4.24353089435013, -1.04920275957987,  
> -7.29644254501929,
> -9.258795, -8.06401751967253, -4.00546526835325, 1.49980094252710,
> 6.27267077967238, 8.41152490344017, 7.02084844979693,  
> 2.35565979843151,
> -3.73136662594352, -9.10401295836816, -11.8667592918697,  
> -11.1209788118259,
> -7.53850052521814, -2.53029214376759, 0, 0, -0.102817113851368,
> -0.308451341554103, -0.514085569256838, -0.719719796959573,  
> -0.925354024662308,
> -1.23322653332276, -1.64333732294093, -2.05344811255911,  
> -2.46355890217728,
> -2.87366969179545, -3.38486435525866, -3.99714289256690), z = c(-1.8,
> 1.59, 4.69619670884637, 7.75612339689976, 11.0469981052342,  
> 14.6030384546677,
> 18.357177803767, 22.4500668968389, 26.4412083543305, 29.9717539469394,
> 32.9734610690970, 35.5800854363887, 37.7984507886837,  
> 40.1485063481344,
> 1.8, 5.19, 8.71511020948371, 12.4692495585831, 16.0252899080165,
> 19.3161646163510, 22.3760913044043, 25.1539112488595,  
> 27.7605356161511,
> 30.7622427383088, 34.2927883309177, 38.2839297884093,  
> 42.5850840652024,
> 47.0021666825248, 0, 3.39, 6.77522880132507, 10.1556864039752,
> 13.5361440066254, 16.9166016092755, 20.2970592119256,  
> 23.6632300791254,
> 27.0151142108747, 30.3669983426241, 33.7188824743734,  
> 37.0707666061228,
> 40.3989288961516, 43.7033693444598), v4 = c(1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("x",
> "y", "z", "v4"), row.names = c(NA, -42L), class = "data.frame")
>
> endPtlength<-length(dnaStr$x)
>
> endPt1<-endPtlength/3
>
> maxX<-max(dnaStr$x)
> minX<-min(dnaStr$x)
> minY<-min(dnaStr$y)
> maxY<-max(dnaStr$y)
> minZ<-min(dnaStr$z)
> maxZ<-max(dnaStr$z)
> difX<-abs(maxX-minX)
> difY<-abs(maxY-minY)
> difZ<-abs(maxZ-minZ)
> angStrong<-round(max(difX,difY,difZ))
> angStrongX<-(angStrong-difX)/2
> angStrongY<-(angStrong-difY)/2
> angStrongZ<-(angStrong-difZ)/2
> minX<-minX-angStrongX
> maxX<-maxX+angStrongX
> minY<-minY-angStrongY
> maxY<-maxY+angStrongY
> minZ<-minZ-angStrongZ
> maxZ<-maxZ+angStrongZ
>
> s3d<-scatterplot3d(dnaStr$x[1:endPt1],dnaStr$y[1:endPt1],dnaStr 
> $z[1:endPt1], color="black",xlim=c(minX,maxX),  
> ylim=c(minY,maxY),zlim=c(minZ,maxZ),grid=FALSE,  
> xlab="",ylab="",zlab="", axis=FALSE, type="l")
>
> s3d$points3d(dnaStr$x[1:endPt1],dnaStr$y[1:endPt1],dnaStr$z[1:endPt1])
>
> s3d$points3d(dnaStr$x[(endPt1+1):(endPt1*2)],dnaStr$y[(endPt1+1): 
> (endPt1*2)],dnaStr$z[(endPt1+1):(endPt1*2)])
> s3d$points3d(dnaStr$x[(endPt1+1):(endPt1*2)],dnaStr$y[(endPt1+1): 
> (endPt1*2)],dnaStr$z[(endPt1+1):(endPt1*2)],type="l")
>
> s3d$points3d(dnaStr$x[(endPt1*2+1):(endPtlength)],dnaStr 
> $y[(endPt1*2+1):(endPtlength)],dnaStr$z[(endPt1*2+1): 
> (endPtlength)],type="l")
>
>
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net]
> Sent: Saturday, September 26, 2009 10:49 PM
> To: Nair, Murlidharan T
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] 3D to 2D projection
>
>
> On Sep 26, 2009, at 10:07 PM, Nair, Murlidharan T wrote:
>
>> Is there a method that I can use to convert 3D coordinates into 2D?
>
> Yes.
>
>> I was looking at persp and trans3d. Are those the ones I should be
>> looking at ?
>
> Yes.
>
>
>> Thanks ../Murli
>>
> -- 
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list