Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot 3D function in r? [duplicate]

Tags:

plot

r

I have a function in 3D - let's say De Jong function:

fdejong <- function (x, y) {
   return (x^2 + y^2)
}

How can I draw it's plot in 3D? I want to achieve an effect similar to this from wikipedia:

http://en.wikipedia.org/wiki/File:Sphere_function_in_3D.pdf

like image 295
SathOkh Avatar asked Jun 05 '14 16:06

SathOkh


People also ask

Can you make 3D graphs in R?

R allows to build three dimensional charts, mainly thanks to the rgl package. Even if 3D is often a bad practice, it can be useful in specific situation.

Can you plot functions in R?

The plot() function in R isn't a single defined function but a placeholder for a family of related functions. The exact function being called will depend upon the parameters used. At its simplest, plot() function simply plots two vectors against each other. This gives a simple plot for y = x^2.


2 Answers

try this :

fdejong <- function (x, y) {
   return (x^2 + y^2)
}


x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, fdejong)
z[is.na(z)] <- 1
op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

fdejong persp plot

like image 166
Vincent Guyader Avatar answered Oct 14 '22 18:10

Vincent Guyader


You can also use the Lattice wireframe function. (Using @user1020027's data)

fdejong <- function (x, y) {
   return (x^2 + y^2)
}

x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, fdejong)
z[is.na(z)] <- 1

require(lattice)
wireframe(z, drape=T, col.regions=rainbow(100))

lattice wireframe color plot

like image 41
MrFlick Avatar answered Oct 14 '22 17:10

MrFlick