Jordan Bush
Jordan Bush

Reputation: 1

How to graph a polygon in 3D in r

I am trying to graph a set of polygons in three dimensional space in R. Each polygon is a (non-regular) k-simplex; that is, each side of the polygon is formed by a triangle. An example is given here:

             x1    y1   z1     x2    y2   z2    x3    y3   z3
triangle1  -2.59  8.44 6.00 -11.31 10.75 1.09 -2.59  8.44 6.00
triangle2   0.19  5.50 0.00  -3.66 10.27 0.61  0.19  5.50 0.00
triangle3 -11.31 10.75 1.09 -10.83 12.02 1.02 -2.73  9.75 1.23
triangle4 -10.83 12.02 1.02   0.19  5.50 0.00 -3.66 10.27 0.61
triangle5   0.19  5.50 0.00  -3.66 10.27 0.61  0.19  5.50 0.00
triangle6 -11.31 10.75 1.09 -10.83 12.02 1.02 -2.73  9.75 1.23
triangle7 -10.83 12.02 1.02  -2.59  8.44 6.00 -3.66 10.27 0.61
triangle8  -2.59  8.44 6.00  -2.73  9.75 1.23 -2.59  8.44 6.00

I have found packages in R that graph points (scatterplot3d) or functions (plot3D) in three dimensions, but I have not found one that graphs polygons. There is a function in MATLAB (fill3) that generates the figure that I am looking for (example pictured below), however I am hoping to perform all of my data analysis (most of which has already been coded in R) in one platform. Is there a package or a function in R that will generate figures of three dimensional polygons similar to those in MATLAB?

Above data visualized as a three dimensional polygon, coded in MATLAB

Upvotes: 0

Views: 815

Answers (1)

C_Z_
C_Z_

Reputation: 7816

The package you are looking for is plot3D, specifically the surf3D function. Example below:

x <- data.matrix(dat[,c("x1", "x2","x3")])
y <- data.matrix(dat[,c("y1", "y2","y3")])
z <- data.matrix(dat[,c("z1", "z2","z3")])


surf3D(x,y,z, colvar=NULL, col="blue", border="black", theta = 75, phi=45, bty="b2", zlim=c(0,6*13), xlim = c(-12,2), ylim=c(5,13))

Upvotes: 1

Related Questions