[R] Ploting a function of two arguments

Liaw, Andy andy_liaw at merck.com
Fri Mar 11 03:48:51 CET 2005


There are at least two options:

1. Use outer(); e.g., something like z <- outer(x, y, f).

2. Use expand.grid(x, y), then call f() with the output.

Andy

> From: Martin C. Martin
> 
> Hi,
> 
> I've written a function:
> 
> myfun <- function(x, y) {
>    // blah blah
> }
> 
> and I want to graph it.  My plan is to use persp(), and my 
> question is: 
> how do I create the array of values?  One possibility is:
> 
> x <- seq(0, 10, by=.1)
> y <- seq(0, 10, by=.1)
> 
> inputs <- <somehow create an array of x,y pairs>
> 
> outputs <- apply(A, c(1,2), myfun)
> 
> The "inputs" array would need to be 101x101x2 array, i.e. 
> basically a 2D 
> array of pairs.  I might need to write a little wrapper for myfun.
> 
> Am I on the right track, or is there an easier way to do 
> this?  It seems 
> there should be an easier way to graph a 2 argument function 
> like this.
> 
> - Martin
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 
>




More information about the R-help mailing list