我发现在R中创建美观的3D曲面很有挑战性。我熟悉解决方案(perspimagewireframelatticergl,,以及SO中其他问题的其他几种解决方案),但结果并不好。
有没有可能像在MATLAB中一样在R中创建三维曲面图?

Here is the MATLAB code
    % Create a grid of x and y points
    points = linspace(-2, 0, 20);
    [X, Y] = meshgrid(points, -points);

    % Define the function Z = f(X,Y)
    Z = 2./exp((X-.5).^2+Y.^2)-2./exp((X+.5).^2+Y.^2);

    % "phong" lighting is good for curved, interpolated surfaces. "gouraud"
    % is also good for curved surfaces
    surf(X, Y, Z); view(30, 30);
    shading interp;
    light;
    lighting phong;
    title('lighting phong', 'FontName', 'Courier', 'FontSize', 14);

情节现代,色彩丰富,美观大方,代码语法可读性强。
这在R基地有可能吗?

最佳答案

jet.colors是Matlab调色板之一的R-答案:

points = seq(-2, 0, length=20)
#create a grid
XY = expand.grid(X=points,Y=-points)
# A z-function
Zf <- function(X,Y){
     2./exp((X-.5)^2+Y^2)-2./exp((X+.5)^2+Y^2);
     }
# populate a surface
Z <- Zf(XY$X, XY$Y)
zlim <- range(Z)
zlen <- zlim[2] - zlim[1] + 1

jet.colors <-   # function from grDevices package
      colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                      "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
colorzjet <- jet.colors(100)  # 100 separate color
require(rgl)
open3d()
rgl.surface(x=points, y=matrix(Z,20),
            coords=c(1,3,2),z=-points,
            color=colorzjet[ findInterval(Z, seq(min(Z), max(Z), length=100))] )
axes3d()
rgl.snapshot("copyMatlabstyle.png")

我承认,获得颜色与“Z轴”(实际上是RGL Y轴)对齐似乎是非常不直观的。如果你想要的光泽,高光效果,你可以发挥与照明角度的Matlab。
您还可以添加或删除照明:
clear3d(type = "lights")
light3d(theta=0, phi=0)
light3d(theta=0, phi=0)  # twice as much light.

之后:
 grid3d("x")
 grid3d("y")
 grid3d("z")

 rgl.snapshot("copyMatlabstyle3.png")

你可以把y网格“放在”曲面后面:
grid3d("y+")

axes3daxis3d调用类似的调整可以移动刻度的位置。
有关更多示例,请查看http://rgm3.lab.nig.ac.jp/RGM/R_image_list并搜索'plot3d',这将显示R2BayesX::plot3d函数的示例,查看Karline Soetaert的plot3d包vignette,"50 ways to plot a volcano"

关于r - 如何创建3D-MATLAB样式-R中的曲面图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20549540/

10-12 14:04