【问题标题】:Increasing 3D surface plot thickness using the Plot3D package in R使用 R 中的 Plot3D 包增加 3D 表面绘图厚度
【发布时间】:2018-03-27 20:50:52
【问题描述】:

有没有办法使用 R 中 plot3D 包中的 persp3D() 函数来增加 3D 表面绘图厚度,例如绘图 here?似乎在文档中找不到任何内容。

【问题讨论】:

    标签: r rgl plot3d


    【解决方案1】:

    我能够通过创建多个表面并使用 plot3D 函数的curtain 参数连接它们来实现此功能。厚度由每个表面的z 参数控制。如果有人需要示例代码,这里是;

    为简单起见,我使用米来指定表面的厚度/高度

    library(plot3D)
    library(plot3Drgl)
    
    # volcano data range
    clim <- range(volcano)
    
    # Surface 1
    persp3D(z = volcano, zlim = c(0, 600), clim = clim, 
            box = FALSE, plot = FALSE, curtain = TRUE, border = "black")
    
    # Surface 2 - 20m below surface 1
    persp3D(z = volcano - 20, clim = clim, colvar = volcano, 
            add = TRUE, colkey = FALSE, plot = FALSE, curtain = TRUE, border = "black")
    
    # Surface 3 - 40m below surface 1
    persp3D(z = volcano - 40, clim = clim, colvar = volcano, 
            add = TRUE, colkey = FALSE, plot = FALSE, curtain = TRUE, border = "black")
    
    # Surface 4 - 60m below surface 1
    persp3D(z = volcano - 60, clim = clim, colvar = volcano, 
            add = TRUE, colkey = FALSE, plot = TRUE, curtain = TRUE, border = "black")
    
    # Surface 5 - 80m below surface 1
    persp3D(z = volcano - 80, clim = clim, colvar = volcano, 
            add = TRUE, colkey = FALSE, plot = TRUE, curtain = FALSE, border = "black")
    
    # Open rgl for interactive viewing
    plotrgl()
    

    【讨论】:

    • 真好看。 (我添加了library() 调用以使其更易于其他人使用。)
    【解决方案2】:

    我不知道plot3D,但rgl 没有专门的功能。这样做的方法是计算对应于平板边缘的多边形,并使用polygon3d 绘制这些多边形。例如:

    data(volcano)
    persp3d(volcano, col = "green")
    
    # The bottom of the slab
    persp3d(volcano-10, col = "blue", add = TRUE)
    
    minx <- 0
    maxx <- 1
    miny <- 0
    maxy <- 1
    
    m <- nrow(volcano)
    n <- ncol(volcano)
    
    # The front edge
    edgex <- c(seq(minx, maxx, length.out = m),
               seq(maxx, minx, length.out = m))
    edgey <- miny
    edgez <- c(volcano[,1],rev(volcano[,1] - 10))
    polygon3d(cbind(edgex, edgey, edgez), coords = c(1,3), 
              col = "yellow")
    
    # The back edge
    edgey <- maxy
    edgez <- c(volcano[,n],rev(volcano[,n] - 10))
    polygon3d(cbind(edgex, edgey, edgez), coords = c(1,3), col = "white")
    
    edgex <- minx
    edgey <- c(seq(miny, maxy, length.out = n),
               seq(maxy, miny, length.out = n))
    edgez <- c(volcano[1,],rev(volcano[1,] - 10))
    polygon3d(cbind(edgex, edgey, edgez), coords = c(2,3), col = "black")
    
    edgex <- maxx
    edgez <- c(volcano[m,],rev(volcano[m,] - 10))
    polygon3d(cbind(edgex, edgey, edgez), coords = c(2,3), col = "green")
    

    【讨论】:

    • 感谢@user2554330 的回复。我在运行polygon3d(cbind(edgex, edgey, edgez), coords = c(2,3), col = "black")polygon3d(cbind(edgex, edgey, edgez), coords = c(1,3), col = "yellow") 时遇到了以下错误Error in processOutside(i) : Cannot triangulate polygon。我已尝试修复代码,但无济于事。
    • 是的,三角测量算法可能会失败。通常,如果您再试一次,它将起作用,因为它通常具有随机分量。但是,如果多边形设置不正确,它可能永远不会成功,因此值得检查您的边缘向量,例如如果遇到问题,请致电plot(edgex, edgez, type="l")
    • 非常感谢@user2554330。我会玩弄向量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多