简体   繁体   English

在R的绘图函数中输入参数

[英]Inputting arguments in plot function of R

How do I plot a function which has more than one argument in R. 我如何绘制一个在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. 我想在y = 2时绘制函数fn1,但是plot(fn1,y = 2)不起作用。 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: 如果您真正想要的是在y = 2时看到fn1的值,则应使用单个参数:

fn1 <- function(x) {
  sin(x + 2) 
}

And then plot it across an index in the x that you created 然后将其绘制在您创建的x的索引中

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 . 第二个参数是指出,Y = 2 的情节 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM