在ggplot2中绘制一个函数,其中包含多于x的参数

net*_*ker 5 r function ggplot2

我想提请幂律函数依赖于三个参数:x,agamma.该函数如下所示:

powerlaw <- function(x, a, gamma){
   a*(x**(-gamma))
}
Run Code Online (Sandbox Code Playgroud)

现在我想绘制这个但我无法弄清楚如何指定agamma告诉R使用所选范围x.我试过这个:

require(ggplot2)
qplot(c(1,10), stat="function", fun=powerlaw(x, a=1, gamma=1), geom="line")
Run Code Online (Sandbox Code Playgroud)

但它说

Error in (x^(-gamma)): x is missing  
Run Code Online (Sandbox Code Playgroud)

当然,以下代码通过修复agamma:

powerlaw1 <- function(x){
   1*(x**(-1))
}
qplot(c(1,10), stat="function", fun=powerlaw1, geom="line")
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

csg*_*pie 3

您需要单独指定参数:

qplot(x=c(1,10), stat="function", 
      fun=powerlaw, geom="line", 
      arg=list(a=1, gamma=1))
Run Code Online (Sandbox Code Playgroud)

请参阅?stat_function了解更多详情。