在ggplot2中用多于x作为参数绘制一个函数 [英] Draw a function in ggplot2 with more than x as parameter

查看:125
本文介绍了在ggplot2中用多于x作为参数绘制一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个依赖三个参数的幂律函数: 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆