Plotting 3D region in R

2019-09-25 07:09发布

问题:

I have three variables, x, y and z, and I would like to plot the following region (shaded) in a 3D plot:

1 < x < 4, 5 < y < 10, -6 <z <-2

Is there any way I can do that?

Thank you!

回答1:

Here is the code:

library(rgl)
c3d <- cube3d(color="red",alpha=.1)
c3d$vb[1,] <- c3d$vb[1,] *1.5+2.5
c3d$vb[2,] <- c3d$vb[2,] *2.5+7.5
c3d$vb[3,] <- c3d$vb[3,] * 2 - 4
shade3d(c3d)
axes3d()

The tricky point: the original cube c3d is -1 <= x,y,z <= 1. The corners are listed in c3d$vb as 8 columns. I converted the original corners to the new corners. For instance about the x-axis (first row of c3d$vb) we want to convert (-1,1) to (1, 4). This is done by a factor of (4-1)/(1-(-1))=1.5, which converts (-1,1) to (-1.5, 1.5). Then by adding 4-1.5 = 2.5 we will have (1,4).

If you are interested to have the axes origin in your plot, you may add plot3d(0,0,0) before shade3d(c3d) - I am sure there are better solutions to this - and you will have:



标签: r plot 3d