简体   繁体   English

从R中的2D函数绘制3D线性曲面

[英]Plot a 3D linear surface from 2D functions in R

I have the following data: 我有以下数据:

x <- c(1000,2000,3000,4000,5000,6000,8000,12000)

y_80 <- c(33276,33276,5913,2921,1052,411,219,146)
y_60 <- c(14724,14724,3755,1958,852,372,211,140)
y_40 <- c(9632,9632,2315, 1250,690,332,196,127)
y_20 <- c(4672,4672,1051,562,387,213,129,81)
y_5  <- c(825,825,210,118,88,44,27,17)

From this data I create 5 spline functions: 根据这些数据,我创建了5个样条函数:

f_80 <- splinefun(x, y_80, method=c("monoH.FC"))
f_60 <- splinefun(x, y_60, method=c("monoH.FC"))
f_40 <- splinefun(x, y_40, method=c("monoH.FC"))
f_20 <- splinefun(x, y_20, method=c("monoH.FC"))
f_5 <- splinefun(x, y_5, method=c("monoH.FC"))

The Z axle is the number after the f_ ie for function f_80 the Z value is 80 and so on. Z轴是f_之后的数字,即对于函数f_80,Z值为80,依此类推。

What I needed to do is to plot a 3D surface from those functions, with linear interpolation between the lines. 我需要做的是根据这些函数绘制3D曲面,并在线之间进行线性插值。 Is that possible in R? R中有可能吗? I have already SO similar questions but I can't find an answer. 我已经有类似的问题,但找不到答案。 Thanks 谢谢

There are a variety of R pseudo-3d plotting functions. 有多种R伪3d绘图功能。 base-graphics has persp , and wireframe is in Lattice, and the coolest of all, surface3d is in rgl. 基本图形具有perspwireframe位于Lattice中,最酷的surface3d位于rgl中。 They differ in how they handle the axis arguments but they all generally accept a matrix argument for the z-values. 它们在处理轴参数方面有所不同,但通常都接受z值的矩阵参数。 `persp is simplest: `persp最简单:

 z_80 <- f_80(x)
 z_60 <- f_60(x)
 z_40 <- f_40(x)
 z_20 <- f_20(x)
 z_5  <- f_5(x) 

 png(); persp(matrix(c(z_80, z_60, z_40, z_20, z_5 ), nrow=length(x), 
              dimnames= list(X=x, Y=c(80,60,40,20,5) ) ) ) 
 dev.off()

在此处输入图片说明

You might find that using ticktype="detailed" is more informative: 您可能会发现使用ticktype="detailed"更有用:

 png(); persp(matrix(c(z_80,z_60,z_40,z_20,z_5 ), nrow=length(x), 
                      dimnames=list(X=x, Y=c(80,60,40,20,5) ) ) , 
                      ticktype="detailed", xlab="X axis", ylab="Y axis", 
                      zlab=".                Z= f(y)", theta=45) ; dev.off()

I generally need to play with viewing angle theta to get something I like. 我通常需要使用视角theta才能获得喜欢的东西。 Getting the z-axis label to move away from the long z-tick-labels was a hack. 让z轴标签远离长的tick标签是一个hack。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM