I'm attempting to plot a surface in 3 dimensions using plot3D
but I'm running into problems. I'm hoping you guys could help or at least point me in the right direction. I'm open to using a different package if that's the solution.
I'm reading in a tab-delimited text file with a couple of dozen columns, each is it's own variable, and each row is a single observation (I hope that makes sense).
Let's say r_NH4
is what I want to map to the x axis, r_NO3
is y, removal_N
is z
.
plot3D
requires x and y to be monotonically increasing if your input is a matrix. The structure of my data, and the size, make it difficult to sort both increasing (at least I haven't been able to figure out how to do it). Reading the man page I learned that lists can be used to input x
, y
, z
all at once (with no reference to order).
I create a list for plot3d to interpret:
list.r_conc<-list(x=r_NH4,y=r_NO3,z=removal_N,3)
And I try to plot in plot3d
.
library(plot3D)
plot3d(list.r_conc,
xlab="x",
ylab="y",
zlab="z",
xlim=c(0,40),
ylim=c(0,40)
)
The result creates an rgl
object that plots each of the points as a scatter plot, not as a surface.
Is there a way to turn this into a surface by adding an argument to the plot3D
command?
Is the fact that my data is discrete and/or unordered preventing the surface from being plotted?
Searching for a solution led me to this question. Trying to follow the solution I tried the following script:
z=as.matrix(data.frame(r_NH4,r_NO3))
z=matrix(removal_N,ncol=length(r_NH4),nrow=(r_NO3))
persp3d(z=z)
The output was a jumbled mess of planes and the axis for r_NH4
and r_NO3
were odd/not consistent with the input (but at least it's not creating a scatterplot!).
Is there a way to sort by both x
and y
variables while keeping z
associated with the correct coordinates?
Is plot3D even capable of plotting a surface using discrete values (instead of a model or function)?
r_NH4 r_NO3 removal_N
2.220 16.816 1.609
1.734 13.280 4.262
0.000 21.586 -3.240
25.166 3.111 3.479
25.652 2.887 4.708
31.751 3.021 -4.899
17.042 4.868 3.118
23.259 15.917 2.968
23.560 15.773 3.250
24.973 14.533 0.848
25.452 13.973 2.880
21.599 11.528 -4.727
25.243 13.193 0.950
25.049 13.763 -1.134
25.434 14.550 2.152
26.499 15.446 0.606
25.661 15.207 2.449
19.422 10.645 2.350