在R中绘制函数:如何使用数据表中的参数值绘制多条曲线?(Graphing a function in R: how do I draw many curves using parameter valu

系统教程 行业动态 更新时间:2024-06-14 17:01:34
在R中绘制函数:如何使用数据表中的参数值绘制多条曲线?(Graphing a function in R: how do I draw many curves using parameter values in a data table?)

我是新手使用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我想一次在同一图表上绘制所有这些曲线? 提前致谢!!

I'm new to using R. I have a function with 4 parameters (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)

I no problem plotting that function by manually specifying values for each parameter.

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

Ultimately, i will want to plot thousands of curves, so I want to be able to tell R that the parameter estimates are in a table. Each row is the data for one curve

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

How do I tell R that I want to plot all of these curves on the same graph at once? Thanks in advance!!

最满意答案

下面是一种创建泛型函数的方法,该函数可以使用公式和变量的data.frame(列名称需要匹配formla中使用的参数)使用quote和eval

## 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)

在此处输入图像描述

Here is one way to create a generic function that can be used with a formula and a data.frame of variables (column names need to match arguments used in formla) using quote and eval

## 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

更多推荐

本文发布于:2023-04-20 18:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6ae9811da9a5041b5f1b38263162348c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   多条   函数   数据表   曲线

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!