如何在 R 中创建 3D - MATLAB 风格 - 曲面图 [英] How to create 3D - MATLAB style - surface plots in R

查看:27
本文介绍了如何在 R 中创建 3D - MATLAB 风格 - 曲面图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在 R 中创建美观的 3D 表面具有挑战性.我熟悉解决方案(perspimagewireframelatticergl 和 SO 中其他问题中的其他几个解决方案),但结果并不好.

I find it challenging to create aesthetically pleasing 3D surfaces in R. I am familiar with the solutions (persp, image, wireframe, lattice, rgl and several other solutions in other questions in SO), but the results are not nice.

是否可以像在 MATLAB 中一样在 R 中创建 3D 曲面图?

Is it possible to create 3D surface plots in R like in MATLAB?

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);

情节现代,色彩丰富,美观,代码语法非常易读.

The plot is modern, colorful, aesthetically pleasing, the code syntax is very readable.

这在基础 R 中可能吗?

Is this possible in base R?

推荐答案

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

jet.colors is the R-answer to one of hte Matlab color palettes:

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 提供的闪亮、镜面反射效果,您可以调整照明角度.

I will admit that getting the colors to line up with the "Z-axis" (which is actually the rgl y-axis) seemed very unintuitive. If you want the shiny, specular effect that Matlab delivers you can play with the angle of illumination.

您还可以添加或删除照明:

You can also add or remove lighting:

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 网格放在表面后面":

You could have put the y-grid "behind" the surface with:

grid3d("y+")

axes3daxis3d 调用的类似调整可以移动比例尺的位置.

Similar tweaks to the axes3d or axis3d calls could move the location of the scales.

更多示例,请查看http://rgm3.lab.nig.ac.jp/RGM/R_image_list 并搜索plot3d",它会显示 R2BayesX::plot3d 函数的示例, 查看 Karline Soetaert 的 plot3D 包小插图,

For further examples, look at http://rgm3.lab.nig.ac.jp/RGM/R_image_list and search for 'plot3d' which brings up examples of the R2BayesX::plot3d function, Look at Karline Soetaert's plot3D package vignette, "50 ways to plot a volcano"

这篇关于如何在 R 中创建 3D - MATLAB 风格 - 曲面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆