Hi, rgl plots seem to require the z object in the form of a matrix. I would like some help in constructing this matrix when I cannot use the outer function. Let me explain. library(rgl) library(plot3D) x <- 1:10 y <- 1:20 fun1 <- function (x,y) {x^2+y^2} z <- outer(x,y, fun1) open3d() surface3d(x,y,z,col="red",theta=50, phi=20) This works fine. However, when I have a data frame of x, y and z with z as a vector, how do I proceed? z <- x^2+y^2> surface3d(x,y,z,col="red",theta=50, phi=20)Error in surface3d(x, y, z, col = "red", theta = 50, phi = 20) : ??At least one coordinate must be a matrix? How do I get z in the form of a matrix? Thanks.
In the general case you cannot do that... you have to choose a way to interpolate your data points from a known x,y partition like your first example (your interpolation gets used as "f"). In the special case where your points in the data frame were generated as a grid, then you should still have your x and y partition variables from when you created the data frame, and you should be able to construct the matrix using the matrix() constructor (not the as.matrix function) from your data frame z column. ?matrix On May 31, 2025 4:04:20 AM PDT, ravi via R-help <r-help at r-project.org> wrote:>Hi, >rgl plots seem to require the z object in the form of a matrix. I would like some help in constructing this matrix when I cannot use the outer function. Let me explain. >library(rgl) >library(plot3D) >x <- 1:10 >y <- 1:20 >fun1 <- function (x,y) {x^2+y^2} >z <- outer(x,y, fun1) >open3d() >surface3d(x,y,z,col="red",theta=50, phi=20) > >This works fine. However, when I have a data frame of x, y and z with z as a vector, how do I proceed? >z <- x^2+y^2 >> surface3d(x,y,z,col="red",theta=50, phi=20) > >Error in surface3d(x, y, z, col = "red", theta = 50, phi = 20) : >??At least one coordinate must be a matrix? > >How do I get z in the form of a matrix? >Thanks. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide https://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
On 2025-05-31 7:04 a.m., ravi via R-help wrote:> Hi, > rgl plots seem to require the z object in the form of a matrix. I would like some help in constructing this matrix when I cannot use the outer function. Let me explain. > library(rgl) > library(plot3D) > x <- 1:10 > y <- 1:20 > fun1 <- function (x,y) {x^2+y^2} > z <- outer(x,y, fun1) > open3d() > surface3d(x,y,z,col="red",theta=50, phi=20) > > This works fine. However, when I have a data frame of x, y and z with z as a vector, how do I proceed? > z <- x^2+y^2 >> surface3d(x,y,z,col="red",theta=50, phi=20) > > Error in surface3d(x, y, z, col = "red", theta = 50, phi = 20) : > ??At least one coordinate must be a matrix > > How do I get z in the form of a matrix?From a private message, it appears you have a scattering of x and y values, and each has a z. You'd like a plot of the surface z(x, y). It won't work with your sample data because x and y have different lengths, but you can do something like this to triangulate your data and do linear interpolation within each triangle: x <- runif(100, 0, 10) # 100 random points from 0 to 10 y <- runif(100, 0, 20) # 100 random points from 0 to 20 z <- x^2 + y^2 # z as a function of x and y library(deldir) # need this to triangulate the x, y values dxyz <- deldir(x, y, z = z) library(rgl) plot3d(dxyz, col = "red") Use plot3d(dxyz, col = "red", front = "lines") to see the triangulation more clearly. Duncan Murdoch