# [R] plotting an isosurface on a 3d plot

ravi rv15| @end|ng |rom y@hoo@@e
Fri Jun 14 15:14:54 CEST 2019

``` Hi Duncan,Thanks a lot for your help. You have really opened up a road for me to to pursue further.
Your later solution with cylinder3d is interesting. However, in my real application, I have several parallel planes and very irregular contours in each of them. So the quad3d command is more useful.  Are there more efficient ways of using this without the for loops?
Other points where I would like to have help are :    1. I would like to smoothen the surface. But I have not been able to figure out how I can use the addNormals command along with quad3d. Are there other ways of improving the smoothness?
2. As I said before, I can have 5 to 6 parallel planes. Sometimes, the jump between two of them can be too high. So, I am thinking of using an interpolation method at an intermediate plane. Do you have any recommendations? I was thing of using a gam method where I make an additive model for y ~ s(x) + s(z). Then predict y at height z given x. I may have to figure out how I do this for closed curves (two or more values of y for a given x). I just wonder if you can indicate generally the route hat I should follow.
3. How do I make the surface transparent? How do I add an alpha value to the quad3d command?

Once again, I would like to thank you for your fantastic help.Thanking you,Ravi
On Thursday, 13 June 2019, 22:52:07 CEST, Duncan Murdoch <murdoch.duncan using gmail.com> wrote:

On 13/06/2019 4:32 p.m., Duncan Murdoch wrote:
> On 13/06/2019 12:47 p.m., ravi via R-help wrote:
>> Hi,I want to plot a surface joining a circle on a plane with another circle (a little offset w.r.t. the first one) on a parallel plane. This is a very simplified version of my problem.
>> I will explain with the following code :
>> # First, I plot the two circlesorig1 <- 0.4;radius1=0.3;theta <- seq(0,2*pi,pi/8)x1 <- orig1+radius1*cos(theta)y1 <- orig1+radius1*sin(theta)
>> orig2 <- 0.7;radius2=0.2;theta <- seq(0,2*pi,pi/8)x2 <- orig2+radius2*cos(theta)y2 <- orig2+radius2*sin(theta)
>> plot(x1,y1,type='b',col="red",xlim=c(0,1),ylim=c(0,1),asp=1)lines(x2,y2,type="b",col="blue")
>> #### the z coordinates on two parallel plnesz1 <- rep(0,length(x1))z2 <- rep(1,length(x2))
>>
>> I have tried to plot my 3d figure using the packages plot3D, misc3d, rgl etc. All these packages require z as a matrix. In my case, all of x,y and z are vectors. How can I plot this surface? In addition, I would like to add similiar surfaces to the same plot.
>
> It's not true that rgl requires z as a matrix:  that's one possibility,
> but there are others.  Here's one way to do what you want.
>
> # First, compute the two circles
> orig1 <- 0.4;radius1=0.3;theta <- seq(0,2*pi,pi/8)
> x1 <- orig1+radius1*cos(theta)
> y1 <- orig1+radius1*sin(theta)
> orig2 <- 0.7;radius2=0.2
> x2 <- orig2+radius2*cos(theta)
> y2 <- orig2+radius2*sin(theta)
>
> #### the z coordinates on two parallel plnes
> z1 <- rep(0,length(x1))
> z2 <- rep(1,length(x2))
>
> library(rgl)
> p1 <- cbind(x1,y1,z1)
> plot3d(p1, col="red")
> p2 <- cbind(x2, y2, z2)
> points3d(p2, col="blue")
>
> quads <- matrix(numeric(), 0, 3)
> for (i in seq_along(x1)[-1]) {
>    quads <- rbind(quads, p1[i-1,], p1[i,], p2[i,], p2[i-1,])
> }
>
> There are more efficient ways to do this (the for loop would be slow if
> you had bigger vectors), but this should give you the idea.

Here's one of the more efficient ways:

cylinder3d(rbind(c(0.4, 0.4, 0), c(0.7, 0.7, 1)),
radius = c(0.3, 0.2),
e1 = rbind(c(0,0,1), c(0,0,1)), sides=20) %>%