2

I'm trying to create a 3D plot in R-Project. I know there was a question like this before but I couldn't solve my problems with the answers there.

What I have is:

Vdot_L = c(0,1,2,3,4,5,6,7,8,9,10)
Qdot_verd = c(2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000) 
zeta_ex = 0.4
T_U = 293.15 #K
T_verd = 273.15 #K
T_cond=Vdot_L*2+T_U
epsilon_k = zeta_ex * T_verd/(T_cond - T_verd)
Pfun <- function(a,b) {a/b}
P    <- outer(Qdot_verd, epsilon_k, FUN="Pfun")

What I'd like to create is a colored surface plot with Vdot_L on the x-Axis, Qdot_verd on the y-Axis and P on the z-Axis. I'm thanful for every help.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
ChrisG
  • 333
  • 1
  • 3
  • 10

2 Answers2

4

So something like this??

library(rgl)
zlim        <- range(P,na.rm=T)
zlen        <- zlim[2] - zlim[1] + 1
color.range <- rev(rainbow(zlen))       # height color lookup table
colors      <- color.range[P-zlim[1]+1] # assign colors to heights for each point
persp3d(Vdot_L, Qdot_verd, P, col=colors)
jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • thats perfect, thx! Now I have the problem that I can't adjust the limits of my z axis. what I tried is 'persp3d(Vdot_L, Qdot_verd, P, col=colors, zlim=c(0,500))' but nothing happens... no error message and no reaction in the plot... any ideas? – ChrisG Apr 28 '14 at 07:04
1

Have you investigated the Plot3D package? http://cran.r-project.org/web/packages/plot3D/plot3D.pdf There's a method in here called surf3d which seems like it would do what you want. After importing the package, cast your values to matrix and write:

surf3d(Vdot_L, Qdot_verd, P)

There's also a color parameter which you can adjust.

Alternatively, use rgl, and avoid the matrix issue:

rgl.surface(Vdot_L, Qdot_verd, P)

Also check out these posts for more info: R: 3D surface plot from 2D matrix How to create 3D - MATLAB style - surface plots in R

Community
  • 1
  • 1
Akshay
  • 814
  • 6
  • 19
  • thx for your answer! I already tried this. It gives me the error "'x' should be a matrix". And I don't understand why. – ChrisG Apr 25 '14 at 14:27
  • So x has to be a matrix in this case, so I think you need to cast it using `matrix(Vdot_L, nrow = ?, ncol = ?)` And specify the number of rows/columns. – Akshay Apr 25 '14 at 14:29
  • Here's a question: are you trying to make this like a scatterplot, where each point has x,y,z coordinates of the corresponding values in each data list? – Akshay Apr 25 '14 at 14:31
  • Actually, why don't you use `rgl.surface()` instead? I think that's more in line with what you want. – Akshay Apr 25 '14 at 14:32
  • the rgl.surface sounds exactly like what i'm looking for. But the result what I get in the moment is just a flat surface. it should look more bended. I have to catch up the train. i'll try it more later... thanks! – ChrisG Apr 25 '14 at 14:46
  • It is NOT a flat surface, but rather one with a very small curvature. You need to look at your inputs and understand that the x inputs are going up at roughly the same rate as your y inputs are going down, so not a very dramatic "bend" will be produced. – IRTFM Apr 25 '14 at 18:51