简体   繁体   中英

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:

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.

What I needed to do is to plot a 3D surface from those functions, with linear interpolation between the lines. Is that possible in 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. base-graphics has persp , and wireframe is in Lattice, and the coolest of all, surface3d is in rgl. They differ in how they handle the axis arguments but they all generally accept a matrix argument for the z-values. `persp is simplest:

 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:

 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. Getting the z-axis label to move away from the long z-tick-labels was a hack.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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