r - 在 R : how do I draw many curves using parameter values in a data table? 中绘制函数

标签 r function plot

我是 R 的新手。我有一个带有 4 个参数(c、w、pl、pr)的函数。

curve(1-(pl+(0.5*(pr-pl))*(1+tanh(2*(x-c)/w))),xlim=c(-27,50),ylim=c(0,1),lwd=3,col=2, add=T)

我可以通过手动为每个参数指定值来绘制该函数。

c = 4
w = 6
pl = 0
pr = 1

最终,我会想要绘制数千条曲线,所以我希望能够告诉 R 参数估计值在表格中。每行是一条曲线的数据

hzdata
pl pr  w c
1  1  0  3 0
2  1  0  4 0
3  1  0  5 0
4  1  0  6 0
5  1  0  7 0
6  1  0  8 0
7  1  0  9 0
8  1  0 10 0

我如何告诉 R 我想一次在同一张图上绘制所有这些曲线?提前致谢!!

最佳答案

这是一种使用 quote 创建可与公式和变量数据框一起使用的通用函数的方法(列名需要与 formla 中使用的参数匹配)评估

## Your formula, changed add=add and col=col to allow to vary
form <- quote(
    curve(1-(pl+(0.5*(pr-pl))*(1+tanh(2*(x-c)/w))),xlim=c(-27,50),ylim=c(0,1),lwd=3,col=col,add=add)
)

plotFun <- function(params, form, colors) {
    eval(form, as.list(c(params[1,], col=colors[1], add=F)))  # plot the first
    for (i in 2:nrow(params))                                 # plot the rest, adding to the first
        eval(form, as.list(c(params[i,], col=colors[i], add=T)))
}

colors <- colorRampPalette(c("red", "yellow"))(nrow(hzdata))
plotFun(hzdata, form, colors=colors)

enter image description here

关于r - 在 R : how do I draw many curves using parameter values in a data table? 中绘制函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31325557/

相关文章:

r - 如何更改 R highcharter 直方图上的 bin 宽度

C 在手动填充的数组中搜索值

r - Circlize 迁移图缺少链接

r - 如何在绘图中使用 geom_line 添加连续线

c# - 为 Roland EGX 350 编程

r - 有没有办法将总和添加到 fviz_eig 图中?

R基本函数本征有对称矩阵的问题

C - 使用指向函数的指针没有结果

javascript - 为什么这段 html 和 javascript 代码不起作用?

r - 当 `a ^ b` 和 `a` 都是整数时,为什么 `b` 返回数字?