R中的垂直3D表面

时间:2013-09-12 02:37:07

标签: r rgl

我想使用R包rgl绘制相交曲面。 我有一个3D数据数组,没有问题在常量“z”(数组[,, n])绘制曲面。 但是,我无法弄清楚如何在同一个rgl图上绘制垂直表面(array [n ,,])。

示例:我可以使用以下方式设置数据:

dat <- list()
lengthout <- 20 # number of data in x, y and z
xrange <- seq(0,10,length.out=lengthout) # sets up dimensions
yrange <- xrange
zrange <- xrange

dat$x <- xrange # contain data in list
dat$y <- yrange
dat$z <- array(rnorm(lengthout^3),dim=c(lengthout,lengthout,lengthout)) # create random data

然后绘制数组[,, n]的多个平面部分,颜色如下:

for (n in seq(1,lengthout,4)){
  zlim <- range(dat$z[,,n],na.rm=T) # range of data in slice
  zlen <- diff(zlim) + 1 # 
  colorlut <- rainbow(zlen) # value color lookup table
  colmah <- colorlut[dat$z[,,n] -zlim[1]+1] # assign colors to value at each point
  surface3d(dat$x,dat$y,array(n,dim=c(lengthout,lengthout)), color=colmah,specular="black",alpha=(1-n/lengthout))
}

Output from code

有没有办法旋转各个曲面的轴,以便我可以绘制这样的东西(注意常量“x”而不是“z”):

zlim <- range(dat$z[n,,],na.rm=T) # range of data in slice
zlen <- diff(zlim) + 1 # 
colorlut <- rainbow(zlen) # value color lookup table
colmah <- colorlut[dat$z[n,,] -zlim[1]+1] # assign colors to value at each point    
surface3d(dat$x,dat$y,array(n,dim=c(lengthout,lengthout)), color=colmah,specular="black")

干杯

1 个答案:

答案 0 :(得分:5)

解决:我没有使用surface3d(blah),而是使用了rgl.surface(blah,coords = c(3,1,2)):

dat <- list()
lengthout <- 20 # number of data in x, y and z
xrange <- seq(0,10,length.out=lengthout) # sets up dimensions
yrange <- xrange
zrange <- xrange

dat$x <- xrange # contain data in list
dat$y <- yrange
dat$z <- array(rnorm(lengthout^3),dim=c(lengthout,lengthout,lengthout)) # create random data

n=1
# Plot constant "z"
zlim <- range(dat$z[,,n],na.rm=T) # range of data in slice
zlen <- diff(zlim) + 1 # 
colorlut <- rainbow(zlen) # value color lookup table
colmah <- colorlut[dat$z[,,n] -zlim[1]+1] # assign colors to value at each point
rgl.surface(xrange,yrange,array(n,dim=c(lengthout,lengthout)), color=colmah,specular="black",alpha=(1-n/lengthout),coords=c(1,2,3))

# Plot constant "x"
zlim <- range(dat$z[n,,],na.rm=T) # range of data in slice
zlen <- diff(zlim) + 1 # 
colorlut <- rainbow(zlen) # value color lookup table
colmah <- colorlut[dat$z[n,,] -zlim[1]+1] # assign colors to value at each point
rgl.surface(yrange,zrange,array(n,dim=c(lengthout,lengthout)), color=colmah,specular="black",alpha=(1-n/lengthout),coords=c(3,1,2))

# Plot constant "y"
zlim <- range(dat$z[,n,],na.rm=T) # range of data in slice
zlen <- diff(zlim) + 1 # 
colorlut <- rainbow(zlen) # value color lookup table
colmah <- colorlut[dat$z[,n,] -zlim[1]+1] # assign colors to value at each point
rgl.surface(xrange,zrange,array(n,dim=c(lengthout,lengthout)), color=colmah,specular="black",alpha=(1-n/lengthout),coords=c(1,3,2))
axes3d()

enter image description here