How do I plot a function which has more than one argument in R.
Suppose I have the function:
fn1<-function(x,y){
sin(x+y)
}
I want to plot the function fn1 when y=2 but plot(fn1,y=2) doesn't work. What is the correct command to do so?
Thanks.
If what you really want is to see the value of fn1 when y = 2 you should use a single argument:
fn1 <- function(x) {
sin(x + 2)
}
And then plot it across an index in the x that you created
plot(fn1(seq(0, 2 * pi, 0.2)), 1:length(fn1(seq(0, 2 * pi, 0.2))))
What you have now in your question is a plot function where the first argument has the function you created but no input. And the second argument is stating that y = 2 for the plot . And if you wanted to plot a vector of a length different from one, it is going to default to an index where the first argument is plotted against the order of your first argument. Hope that made sense. :)
您可以简单地将所有参数应用于定义的函数fn1
,例如:
plot(fn1(x=1:100, y=2))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.