[R] rgl 3d surface

Duncan Murdoch murdoch.duncan at gmail.com
Wed Jul 15 13:53:05 CEST 2015


On 15/07/2015 7:16 AM, AURORA GONZALEZ VIDAL wrote:
> Hello.
> 
> I am trying to plot a 3d surface given its equation. The R code is written
> in blue.
> So, let's say that I have the points x,y,z and I plot them. Also, I compute
> its regression surface doing polynomical regression (fit)
> 
> library('rgl')
> x <- c(-32.09652, -28.79491, -25.48977, -23.18746,-20.88934, -18.58220,
> -17.27919)
> y <- c(-32.096, -28.794, -25.489, -23.187,-20.889, -18.582, -17.279)
> z <- c(12.16344, 28.84962, 22.36605, 20.13733, 79.50248, 65.46150,44.52274)
> plot3d(x,y,z, type="s", col="red", size=1)
> 
> fit <- lm(z ~ poly(x,2) + poly(y,2))
> 
> In this way, I obtain the coefficients of the surface
> 
> coef(fit)
> 
>   (Intercept)   poly(x, 2)1   poly(x, 2)2
>  3.900045e+01  1.763363e+06  6.683531e+05
>   poly(y, 2)1   poly(y, 2)2
> -1.763303e+06 -6.683944e+05
> 
> So I want to repressent the surface
> 3.900045e+01 +1.763363e+06*x + 6.683531e+05*x*x
> -1.763303e+06*y-6.683944e+05*y*y
> 
> How could I do it? Any idea??
> 
> Thank you very much!

You need to write a function f of x and y that produces the fitted
values.  I haven't checked, but I'd assume it needs to take vector
inputs and produce a vector of responses.  Then


persp3d(f)

will draw the surface.  See ?persp3d.function for details on setting the
x and y ranges, etc.

Duncan Murdoch



More information about the R-help mailing list