在R曲面图

时间:2016-03-16 18:25:01

标签: r 3d surface rgl geometry-surface

我正在使用persp()创建一个3d图(但我对任何可以完成工作的事情持开放态度)。现在我想添加一个2d字段,以清楚地说明3d绘图高于特定Z值的位置。有没有办法实现这个目标?理想情况下,它最好是半透明表面,你可以看到表面下的质量与结束。

使用persp文档中的示例

f <- function(x, y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }

x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, f)
z[is.na(z)] <- 1

persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
      ltheta = 120, shade = 0.75, ticktype = "detailed",
      xlab = "X", ylab = "Y", zlab = "Sinc( r )"
) 

如何在z轴的某个点插入切割图形的字段?

1 个答案:

答案 0 :(得分:2)

这个怎么样 - 使用rgl包有很多可能性,但它有一个persp3d函数,可以从基础graphics轻松升级。

library(rgl)

f <- function(x, y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, f)
z[is.na(z)] <- 1

persp3d(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
      ltheta = 120, shade = 0.75, ticktype = "detailed",
      xlab = "X", ylab = "Y", zlab = "Sinc( r )")

# Here we add a transparent purple square to mark the top

# x and y mark the corners of the purple square, z is its height
sqdf <- data.frame(x=c(-10,-10,10,10,-10),
                   y=c(-10, 10,10,-10,-10),
                   z=c(5,5,5,5,5))

# now draw the purple square, 
#    note:
#    -  the "add=T" parameter that appends it to the previous 3d-plot
#    -  the coord paramter tells it what two planes to use when 
#        tesselating the polygon into triangles 
#        (a necessary step and expensive to calculate)

polygon3d(sqdf$x,sqdf$y,sqdf$z,coord=c(1,2),alpha=0.5,color="purple",add=T)

产量:

enter image description here