在ggplot2中用多于x作为参数绘制一个函数 [英] Draw a function in ggplot2 with more than x as parameter
问题描述
我想绘制一个依赖三个参数的幂律函数: x
, a
和伽马
。该函数如下所示:
powerlaw < - function(x,a,gamma){
a *(x **( - gamma))
}
现在我想绘制这个,但是我不能在告诉R为 x使用所选范围的同时,指出如何指定
。我试过这个: a
和 gamma
require(ggplot2)
qplot(c(1,10),stat =function ,fun = powerlaw(x,a = 1,gamma = 1),geom =line)
$ c>
当然,下面的代码通过修正 a
和 gamma
:
powerlaw1 < - function(x){
1 *( x **( - 1))
}
qplot(c(1,10),stat =function,fun = powerlaw1,geom =line)
有什么想法?
需要分别指定参数:
pre $ c $ qplot(x = c(1,10),stat =function,
fun = powerlaw,geom =line,
arg = list(a = 1,gamma = 1))
有关更多详情,请参阅?stat_function
。
I would like to draw a power-law function which depends on three parameters: x
, a
, and gamma
. The function looks like this:
powerlaw <- function(x, a, gamma){
a*(x**(-gamma))
}
Now I want to plot this but I cannot figure out how to specifiy a
and gamma
while telling R to use the chosen range for x
. I tried this:
require(ggplot2)
qplot(c(1,10), stat="function", fun=powerlaw(x, a=1, gamma=1), geom="line")
but it says
Error in (x^(-gamma)): x is missing
Of course, the following code works by fixing a
and gamma
:
powerlaw1 <- function(x){
1*(x**(-1))
}
qplot(c(1,10), stat="function", fun=powerlaw1, geom="line")
Any ideas?
You need to specify the arguments separately:
qplot(x=c(1,10), stat="function",
fun=powerlaw, geom="line",
arg=list(a=1, gamma=1))
See ?stat_function
for more details.
这篇关于在ggplot2中用多于x作为参数绘制一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!