3D equivalent of the curve function in R?

Viewed 5322

The curve function in R provides a simple way to plot a function. For example, this will plot a straight line

f1 <- function(x) x
curve(f1, from=-1, to=1)

Is there an equivalent function in R which takes a function with two argument (e.g., x and y) and ranges for both variables and produces a 3D plot?

For example, imagine I had the following function

f2 <- function(x, y) x + y

Is there a command similar to the following?

curve_3d(f2, x_range=c(-1, 1), y_range=c(-1, 1))
4 Answers

You can have an interactive plot with the plot3Drgl package.

library(plot3Drgl)

f <- function(x,y) x+y
x <- y <- seq(-4,4,len=20)
z <- outer(x, y, f)

persp3Drgl(z=z)

enter image description here

Related