Using 'polygon' in a 3d plot

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using 'polygon' in a 3d plot

Ted Freeman
I'm new to R, after many years of using Matlab. I've found the R
function 'polygon' to be nearly equivalent to the Matlab function
'patch'. For example, the R commands:

plot(c(0, 5), c(0, 4), type = 'n', asp = 1, ann = FALSE)
x <- c(1, 2, 2, 1.5, 1)
z <- c(1, 1, 2, 1.7, 2)
polygon(x, z, col = 'green')

produce a plot with a small green shape exactly as I expect. A nearly
identical plot can be made in Matlab with these commands:

x = [1, 2, 2, 1.5, 1];
z = [1, 1, 2, 1.7, 2];
patch(x, z, 'g')
axis([0, 5, 0, 4])
box on

In Matlab I am able to extend this quite easily to a three-dimensional
plot by simply adding one more vector argument to 'patch'. I don't see
how to do this in R using 'polygon'. (I've primarily looked at
scatterplot3d.) Is there another function I can use?

Since I expect that many of you do not use Matlab, I've put two
graphics showing the example above (Plot 1) and a similar
three-dimensional plot (Plot 2) on this page:
http://staff.washington.edu/freeman/patch.html.

It's Plot 2 that I'd like to be able to reproduce in R.

Thanks for any advice!

  -- Ted

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Using 'polygon' in a 3d plot

Henrik Bengtsson-2
Ted Freeman wrote:

> I'm new to R, after many years of using Matlab. I've found the R
> function 'polygon' to be nearly equivalent to the Matlab function
> 'patch'. For example, the R commands:
>
> plot(c(0, 5), c(0, 4), type = 'n', asp = 1, ann = FALSE)
> x <- c(1, 2, 2, 1.5, 1)
> z <- c(1, 1, 2, 1.7, 2)
> polygon(x, z, col = 'green')
>
> produce a plot with a small green shape exactly as I expect. A nearly
> identical plot can be made in Matlab with these commands:
>
> x = [1, 2, 2, 1.5, 1];
> z = [1, 1, 2, 1.7, 2];
> patch(x, z, 'g')
> axis([0, 5, 0, 4])
> box on
>
> In Matlab I am able to extend this quite easily to a three-dimensional
> plot by simply adding one more vector argument to 'patch'. I don't see
> how to do this in R using 'polygon'. (I've primarily looked at
> scatterplot3d.) Is there another function I can use?
>
> Since I expect that many of you do not use Matlab, I've put two
> graphics showing the example above (Plot 1) and a similar
> three-dimensional plot (Plot 2) on this page:
> http://staff.washington.edu/freeman/patch.html.

Hi, I've got a plot3d() and polygon3d() function in the R.basic package
(http://www.braju.com/R/).  Example:

library(R.basic)  # plot3d() and polygon3d()
xb <- c(0, 4, 4, 0, 0)
yb <- c(0, 3, 3, 0, 0)
zb <- c(0, 0, 4, 4, 0)
plot3d(xb,yb,zb, type="l", theta=35, phi=30)
x <- c(0.8, 1.6, 1.6, 1.2, 0.8)
y <- c(0.6, 1.2, 1.2, 0.9, 0.6)
z <- c(1, 1, 2, 1.7, 2)
polygon3d(x,y,z, col="green")

This gives a similar plot to what Matlab creates.

/Henrik

> It's Plot 2 that I'd like to be able to reproduce in R.
>
> Thanks for any advice!
>
>   -- Ted
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Using 'polygon' in a 3d plot

Duncan Murdoch
In reply to this post by Ted Freeman
On 1/4/2006 4:20 PM, Ted Freeman wrote:

> I'm new to R, after many years of using Matlab. I've found the R
> function 'polygon' to be nearly equivalent to the Matlab function
> 'patch'. For example, the R commands:
>
> plot(c(0, 5), c(0, 4), type = 'n', asp = 1, ann = FALSE)
> x <- c(1, 2, 2, 1.5, 1)
> z <- c(1, 1, 2, 1.7, 2)
> polygon(x, z, col = 'green')
>
> produce a plot with a small green shape exactly as I expect. A nearly
> identical plot can be made in Matlab with these commands:
>
> x = [1, 2, 2, 1.5, 1];
> z = [1, 1, 2, 1.7, 2];
> patch(x, z, 'g')
> axis([0, 5, 0, 4])
> box on
>
> In Matlab I am able to extend this quite easily to a three-dimensional
> plot by simply adding one more vector argument to 'patch'. I don't see
> how to do this in R using 'polygon'. (I've primarily looked at
> scatterplot3d.) Is there another function I can use?
>
> Since I expect that many of you do not use Matlab, I've put two
> graphics showing the example above (Plot 1) and a similar
> three-dimensional plot (Plot 2) on this page:
> http://staff.washington.edu/freeman/patch.html.
>
> It's Plot 2 that I'd like to be able to reproduce in R.

scatterplot3d returns xyz.convert, a function that can project 3d
coordinates down to 2d for doing this sort of thing:

xb <- c(0, 4, 4, 0, 0)
yb <- c(0, 3, 3, 0, 0)
zb <- c(0, 0, 4, 4, 0)
s <- scatterplot3d(xb,yb,zb, type='l')

x = c(0.8, 1.6, 1.6, 1.2, 0.8)
y = c(0.6, 1.2, 1.2, 0.9, 0.6)
z = c(1, 1, 2, 1.7, 2)
polygon(s$xyz.convert(x,y,z),col="green")

You have to be careful about hidden lines; things drawn later obscure
things drawn earlier, even if they should really be behind.  But in this
case that doesn't matter, because the polygon doesn't need to be behind
anything.

Another possibility is to use the rgl package, but it doesn't have the
high level plot functions, so the axes are harder to draw.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html