Hi,
I'm new to R (and math ;) Would somebody please be so kind as to direct me
in plotting a 3D sphere?
I tried something in the lines of:
####
y <- x <- seq(-pi, pi, length=pi*10)
f <- function(x,y)
{
z <- sqrt(pi - x^2 - y^2)
#z[is.na(z)] <- 0
z
}
z <- outer(x, y, f)
persp(x, y, z, theta = 120, phi = 30)
####
I've also tried: .... make.surface.grid(...) .. persp( as.surface( grid, z)
) ... with the same result: 'Incomplete' demi sphere and others..
Any suggestions/solutions would be appreaciated.
Regards,
Derick
PS:Merry X-mas ;)
A hemisphere is relatively easy; try the following:
x <- seq(-1, 1, length=21)
Z <- outer(x, x, function(x, y)sqrt(1-x^2-y^2))
persp(x=x, y=x, z=Z)
A contour plot is also relatively easy:
image(x=x, y=x, z=Z)
contour(x=x, y=x, z=Z, add=T)
However, if you want an honest perspective plot of a sphere
complete with the underside, etc., I know of nothing in R that could do
that. S-Plus has "perspp", which could be used. However, that seems
to
be one of the few features available in S-Plus that is not currently
available in R.
hope this helps.
spencer graves
Derick Schoonbee wrote:
> Hi,
>
> I'm new to R (and math ;) Would somebody please be so kind as to
> direct me in plotting a 3D sphere?
>
> I tried something in the lines of:
> ####
> y <- x <- seq(-pi, pi, length=pi*10)
> f <- function(x,y)
> {
> z <- sqrt(pi - x^2 - y^2)
> #z[is.na(z)] <- 0
> z
> }
> z <- outer(x, y, f)
>
> persp(x, y, z, theta = 120, phi = 30)
> ####
>
> I've also tried: .... make.surface.grid(...) .. persp( as.surface(
> grid, z) ) ... with the same result: 'Incomplete' demi sphere and
> others..
>
> Any suggestions/solutions would be appreaciated.
>
> Regards,
> Derick
>
> PS:Merry X-mas ;)
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Thanks for the reply. The results are exactly the same as what I'm getting.
Now I'm thinking in the lines of:
z1 <- outer(x, y, f)
z2 <- -outer(x, y, f)
So, I want to attach the two hemispheres. But then I need to figure out how
append vectors z1 & z2 and then 'feed' this to persp..
hmm, I'll look into it.
Regards,
D.
From: Spencer Graves <spencer.graves at pdf.com>
To: Derick Schoonbee <schoonbee at hotmail.com>
CC: r-help at stat.math.ethz.ch
Subject: Re: [R] Plot a sphere
Date: Thu, 25 Dec 2003 17:38:01 -0800
A hemisphere is relatively easy; try the following:
x <- seq(-1, 1, length=21)
Z <- outer(x, x, function(x, y)sqrt(1-x^2-y^2))
persp(x=x, y=x, z=Z)
A contour plot is also relatively easy:
image(x=x, y=x, z=Z)
contour(x=x, y=x, z=Z, add=T)
However, if you want an honest perspective plot of a sphere complete
with the underside, etc., I know of nothing in R that could do that. S-Plus
has "perspp", which could be used. However, that seems to be one of
the few
features available in S-Plus that is not currently available in R.
hope this helps. spencer graves
Derick Schoonbee wrote:
>Hi,
>
>I'm new to R (and math ;) Would somebody please be so kind as to direct
me
>in plotting a 3D sphere?
>
>I tried something in the lines of:
>####
>y <- x <- seq(-pi, pi, length=pi*10)
>f <- function(x,y)
>{
> z <- sqrt(pi - x^2 - y^2)
> #z[is.na(z)] <- 0
> z
>}
>z <- outer(x, y, f)
>
>persp(x, y, z, theta = 120, phi = 30)
>####
>
>I've also tried: .... make.surface.grid(...) .. persp( as.surface( grid,
z)
>) ... with the same result: 'Incomplete' demi sphere and others..
>
>Any suggestions/solutions would be appreaciated.
>
>Regards,
>Derick
>
>PS:Merry X-mas ;)
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Uwe,
Thanks, I installed it and ran:
rgl.spheres(rnorm(10),rnorm(10),rnorm(10),radius=1,color=rainbow(10))
Nice :)
Thus, I'll give rgl a try but still would be 'stuborn' to see if I
can
figure a way out to use persp.
Regards,
D.
From: Uwe Ligges <ligges at statistik.uni-dortmund.de>
To: Spencer Graves <spencer.graves at pdf.com>
CC: Derick Schoonbee <schoonbee at hotmail.com>,r-help at
stat.math.ethz.ch
Subject: Re: [R] Plot a sphere
Date: Fri, 26 Dec 2003 12:48:49 +0100
Spencer Graves wrote:
>
> A hemisphere is relatively easy; try the following:
>
> x <- seq(-1, 1, length=21)
> Z <- outer(x, x, function(x, y)sqrt(1-x^2-y^2))
> persp(x=x, y=x, z=Z)
>
> A contour plot is also relatively easy:
>
> image(x=x, y=x, z=Z)
> contour(x=x, y=x, z=Z, add=T)
>
> However, if you want an honest perspective plot of a sphere
> complete with the underside, etc., I know of nothing in R that could do
> that. S-Plus has "perspp", which could be used. However, that
seems to
> be one of the few features available in S-Plus that is not currently
> available in R.
The R package "rgl" by Adler and Nenadic can plot spheres. It is
available at
http://wsopuppenkiste.wiso.uni-goettingen.de/~dadler/rgl/
--- and looks like it will shortly become a CRAN package.
Uwe Ligges
> hope this helps.
> spencer graves
>
> Derick Schoonbee wrote:
>
> > Hi,
> >
> > I'm new to R (and math ;) Would somebody please be so kind as to
> > direct me in plotting a 3D sphere?
> >
> > I tried something in the lines of:
> > ####
> > y <- x <- seq(-pi, pi, length=pi*10)
> > f <- function(x,y)
> > {
> > z <- sqrt(pi - x^2 - y^2)
> > #z[is.na(z)] <- 0
> > z
> > }
> > z <- outer(x, y, f)
> >
> > persp(x, y, z, theta = 120, phi = 30)
> > ####
> >
> > I've also tried: .... make.surface.grid(...) .. persp(
as.surface(
> > grid, z) ) ... with the same result: 'Incomplete' demi sphere
and
> > others..
> >
> > Any suggestions/solutions would be appreaciated.
> >
> > Regards,
> > Derick
> >
> > PS:Merry X-mas ;)
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
To plot the sphere in R I find the best scatterplot3d package as follows: library(scatterplot3d) a=seq(-pi,pi, length=100) x=c(rep(1, 100) %*% t(cos(a))) y=c(cos(a) %*% t(sin(a))) z=c(sin(a) %*% t(sin(a))) scatterplot3d(x, y, z, type="l") Best regards Branimir K. Hackenberger
Derick Schoonbee <schoonbee at hotmail.com> wrote:> Would somebody please be so kind as to direct me in plotting a 3D sphere?Here's one way. I generate an empty 3D plot with "persp", then fill it with polygons transformed with "trans3d" (as found in the help for "persp"). I didn't do hidden surface removal (you didn't mention whether you wanted it), but if you do, just reorder the polygons from "back" to "front" and paint them a solid color (e.g. col="red"), so hidden ones get painted over. pmat <- persp(0:1, 0:1, matrix(,2,2), xlim=c(-1,1), ylim=c(-1,1), zlim=c(-1,1), theta=25, phi=30, expand=.9, xlab="X", ylab="Y", zlab="Z") trans3d <- function(x,y,z, pmat) { # From the help for "persp" tr <- cbind(x,y,z,1) %*% pmat list(x = tr[,1]/tr[,4], y= tr[,2]/tr[,4]) } theta <- seq(0, 2*pi, length=51) phi <- seq(0, pi, length=26) x <- cos(theta) %o% sin(phi) y <- sin(theta) %o% sin(phi) z <- rep(1, length(theta)) %o% cos(phi) for (j in seq(phi)[-1]) for (i in seq(theta)[-1]) { idx <- rbind(c(i-1,j-1), c(i,j-1), c(i,j), c(i-1,j)) polygon(trans3d(x[idx], y[idx], z[idx], pmat)) } -- -- David Brahm (brahm at alum.mit.edu)
Hi,
Thank you all for the solutions.. I now have 3 ways of plotting a
'perfect'
sphere :)
1> rgl
2> scatterplot3d
3> persp (with polygon) as below.
Personally I like the transformation solution as in 3. Thanks a mill.
Derick Schoonbee
From: David Brahm <brahm at alum.mit.edu>
Reply-To: brahm at alum.mit.edu
To: r-help at stat.math.ethz.ch
CC: schoonbee at hotmail.com
Subject: Re: [R] Plot a sphere
Date: Fri, 26 Dec 2003 10:29:08 -0500
Derick Schoonbee <schoonbee at hotmail.com> wrote:
> Would somebody please be so kind as to direct me in plotting a 3D sphere?
Here's one way. I generate an empty 3D plot with "persp", then
fill it with
polygons transformed with "trans3d" (as found in the help for
"persp"). I
didn't do hidden surface removal (you didn't mention whether you wanted
it),
but if you do, just reorder the polygons from "back" to
"front" and paint
them
a solid color (e.g. col="red"), so hidden ones get painted over.
pmat <- persp(0:1, 0:1, matrix(,2,2), xlim=c(-1,1), ylim=c(-1,1),
zlim=c(-1,1),
theta=25, phi=30, expand=.9, xlab="X",
ylab="Y", zlab="Z")
trans3d <- function(x,y,z, pmat) { # From the help for
"persp"
tr <- cbind(x,y,z,1) %*% pmat
list(x = tr[,1]/tr[,4], y= tr[,2]/tr[,4])
}
theta <- seq(0, 2*pi, length=51)
phi <- seq(0, pi, length=26)
x <- cos(theta) %o% sin(phi)
y <- sin(theta) %o% sin(phi)
z <- rep(1, length(theta)) %o% cos(phi)
for (j in seq(phi)[-1]) for (i in seq(theta)[-1]) {
idx <- rbind(c(i-1,j-1), c(i,j-1), c(i,j), c(i-1,j))
polygon(trans3d(x[idx], y[idx], z[idx], pmat))
}
--
-- David Brahm (brahm at alum.mit.edu)