传递参数以便用ggplot stat_function绘图 [英] passing arguments to function with a view to plotting with ggplot stat_function
问题描述
F < - 函数(a,b,...)
/ pre>我有一个函数和一个参数列表。 ){a ^ b + b / a}
L < - 列表(a= 5,b= 2,c= 0)
我想用一个未知的x(或x)和plot来替换其中一个参数(a,b或c)与ggplot的stat_function。
这些计算是一个闪亮的应用程序的一部分,其中用户将1)从下拉列表中选择一个参数,说a,以是未知数,2)使用滑块来选择其他参数的值。 L中的数字5,2,0是用户交互之前要使用的默认参数值。有几个这样的功能。在这里,参数L的列表中有一个F中没有使用的元素。
我一直坚持这么久,直到现在我再也想不起来了。在我尝试的很多事情中,有一个是这样的:
#选择一个参数以改变:
Y< - a
f1 < - 函数(f = F,l = L,y = Y,x,...){
l [y] < - x#用a代替x
do.call(f,l,...)
}
#make a stat_function ggplot:
library(ggplot2)
df < - data.frame(x = c(0,10))
p <-ggplot(df,aes(x))
p <-p + stat_function(fun = f1)
print(p)
这会返回以下错误:
错误(函数(f = F,l = L,y = Y,x,...):缺少
参数x没有默认
as.environment(where)中的错误:'where'is missing
我已经尝试了几个变体,其中包括:设置l [y]< - x,并使用aes_string而不是aes,我也尝试了x的反引号,我已经阅读了有关环境的文档,所以我尝试过定义环境,包装x和几乎永远凡是评估或报价。我甚至尝试过巫毒。我已经失去了在这个上花了多少小时的计数。没有解释的阅读手册或提示的建议会杀了我。 8-)如果我的问题不清楚,请让我知道,我会澄清。谢谢!
解决方案如果我明白,拥有多参数函数,您希望引入一个部分函数,修复别人。试试这个例子:
F < - 函数(a,b,...){a ^ b + b / a}
L < - list(a= 5,b= 2,c= 0)
f.partial < - function(var =a ,params = L){
params [[var]] = as.name(x)
函数(x)do.call(F,params)
}
我们可以测试这个例子:
##变化
f.partial(a)(1)
[1] 3
> F(1,b = L $ b)
[1] 3
##变化b
> f.partial(b)(1)
[1] 5.2
> F(1,a = L $ a)
[1] 5.2
code> ggplot2 :
library(ggplot2)
df< ; - data.frame(x = seq(0,1,0.1))
ggplot(df,aes(x))+
stat_function(fun = f.partial(a),col = 'blue')+
stat_function(fun = f.partial(b),col ='red')
I have a function and a list of arguments.
F <- function(a,b,...) {a^b+b/a} L <- list("a" = 5, "b" = 2, "c" = 0)
I want to replace one of the arguments ("a", "b" or "c") with an unknown x (or "x") and plot with ggplot's stat_function.
These computations are part of a shiny app, where the user will 1) select a parameter from a drop-down list, say "a", to be the unknown, and 2) use sliders to select values of the other parameters. The numbers 5, 2, 0 in L are the default parameter values, to be used before user interaction. There are several such functions. Here the list of parameters L has an element not used in F.
I've been stuck on this for so long that I can't think straight anymore. Of the many things I've tried, here's one:
# select a parameter to vary: Y <- "a" f1 <- function(f = F, l = L, y = Y, x, ...){ l[y] <- x # replace "a" with x do.call(f, l, ...) } # make a stat_function ggplot: library("ggplot2") df <- data.frame(x = c(0,10)) p <- ggplot(df, aes(x)) p <- p + stat_function(fun = f1) print(p)
This returns the following error:
Error in (function (f = F, l = L, y = Y, x, ...) : argument "x" is missing, with no default Error in as.environment(where) : 'where' is missing
I have tried several variants, including: setting l[y] <- "x" and using aes_string instead of aes. I have also tried backquotes around x. I have read through the documentation about environments, so I've tried defining an environment, wrapping x and just about everything around eval or quote. I've even tried voodoo. I've lost count of how many hours I've spent on this. A suggestion to read the manual or a hint without an explanation will kill me. 8-) If my question is unclear, please let me know and I will clarify. Thanks!
解决方案If I understand, Having a multi parameters functions , you want to induce a partial function where you vary one parameter and fix others. Try this for example:
F <- function(a,b,...) {a^b+b/a} L <- list("a" = 5, "b" = 2, "c" = 0) f.partial <- function( var = "a",params=L){ params[[var]]=as.name("x") function(x)do.call(F,params) }
We can test this for example:
## vary a f.partial("a")(1) [1] 3 > F(1,b=L$b) [1] 3 ## vary b > f.partial("b")(1) [1] 5.2 > F(1,a=L$a) [1] 5.2
Testing with
ggplot2
:library("ggplot2") df <- data.frame(x = seq(0,1,0.1)) ggplot(df, aes(x)) + stat_function(fun = f.partial("a"),col='blue') + stat_function(fun = f.partial("b"),col='red')
这篇关于传递参数以便用ggplot stat_function绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!