在ggplot2中叠加多个stat_function调用

发布于 2024-09-12 15:59:24 字数 1063 浏览 3 评论 0原文

我有两个数据框 rawcoef

  • 一个包含原始数据
  • ,另一个包含我从原始数据导出的建模系数。

第一个数据帧raw包含:

  • 时间(0到900秒)
  • 许多变体和四次运行的

OD。第二个数据帧 coef 包含:

  • 每个变体/运行组合一行,以及各个系数(MD.1t0.1) 在该行中。

我已经绘制了每个变体的原始数据分割图,并按 runID 着色,没有出现任何问题。但是,现在我想根据 runID 覆盖模型曲线。

由于建模系数位于不同的数据帧中,具有不同的维度,因此我不能简单地cbind它们。 stat_function 对我不起作用。我一次只能显示一条曲线。

我尝试过使用for循环,每次添加一个stat_function层:

p <- ggplot(temp, aes(Time, OD)) + geom_point(aes(colour = runID), size = 2) #works fine!
calc <- function(x){temp.n$M[ID] * (1 - exp(temp.n$D.1[ID] * temp.n$t0.1[ID] - x)))}
for(ID in 1:length(unique(temp.n$runID))) {
  p <- p + stat_function(fun = calc)
}
print(p)

最后,所有p返回的是原始数据的图,以及循环位的最终曲线。每次我尝试添加新的 stat_function 层时,p 似乎都会恢复到其原始状态。

有什么想法吗?

I have two data frames raw and coef:

  • one containing raw data
  • the other containing modelling coefficients that I have derived from the raw data.

The first data frame raw contains :

  • Time (0 to 900 seconds)
  • OD for many Variants and four runs.

The second data frame coef contains :

  • one row per Variant/run combination, with the individual coefficients (M, D.1 and t0.1) in that row.

I have plotted the raw data split per Variant and colored by runID, without a problem. But, now I want to overlay the model curves according to the runID.

Since the modelling coefficients are in a different data frames, with different dimensions, I can't just cbind them. stat_function won't work for me. I can get only one curve showing at a time.

I have tried with a for loop, adding a stat_function layer each time:

p <- ggplot(temp, aes(Time, OD)) + geom_point(aes(colour = runID), size = 2) #works fine!
calc <- function(x){temp.n$M[ID] * (1 - exp(temp.n$D.1[ID] * temp.n$t0.1[ID] - x)))}
for(ID in 1:length(unique(temp.n$runID))) {
  p <- p + stat_function(fun = calc)
}
print(p)

At the end, all p returns is the plot of the raw data, and the final curve from the looping bit. p seems to revert to its original state every time I try to add a new stat_function layer.

Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-09-19 15:59:24

按照此处给出的解决方案 ,你可能需要自己模仿stat_function的效果。由于您没有给出可重现的示例,因此我创建了一个简单的示例,希望能够模拟您的问题:

library(ggplot2)
reg.fun <- function(x, par1, par2){exp(-x*par1) + par2} #functional form
reg <- data.frame(g=factor(1:3), par1=(1:3)/10, par2=1:3)  #parameters for 3 groups

#generate data from reg.fun
dd <- expand.grid(x=0:9, g=reg$g)         #set x values, and 3 groups from reg
dd <- merge(dd, reg)                      #"import" parameters
dd$mn <- with(dd, reg.fun(x, par1, par2)) #value of function for given x's
dd$y <- rnorm(30, mean=dd$mn, sd=0.5)     #add variability
dd <- subset(dd, select=c(g,x,y))         #remove auxiliary variables 

#similarly to above generate values for the function on a fine grid of x values
pred.dd <- expand.grid(x=seq(0,9, length=101), g=levels(dd$g))
pred.dd <- merge(pred.dd, reg)
pred.dd$y <- with(pred.dd, reg.fun(x, par1, par2))

#draw the plot
p <- qplot(x,y, colour=g, data=dd)  #scatterplot of data
p + geom_line(data=pred.dd)         #add the curves of the functions 

Following on the solution given here, you might have to imitate the effect of stat_function yourself. Since you do not give a reproducible example, I created a simple one that hopefully mimics your problem:

library(ggplot2)
reg.fun <- function(x, par1, par2){exp(-x*par1) + par2} #functional form
reg <- data.frame(g=factor(1:3), par1=(1:3)/10, par2=1:3)  #parameters for 3 groups

#generate data from reg.fun
dd <- expand.grid(x=0:9, g=reg$g)         #set x values, and 3 groups from reg
dd <- merge(dd, reg)                      #"import" parameters
dd$mn <- with(dd, reg.fun(x, par1, par2)) #value of function for given x's
dd$y <- rnorm(30, mean=dd$mn, sd=0.5)     #add variability
dd <- subset(dd, select=c(g,x,y))         #remove auxiliary variables 

#similarly to above generate values for the function on a fine grid of x values
pred.dd <- expand.grid(x=seq(0,9, length=101), g=levels(dd$g))
pred.dd <- merge(pred.dd, reg)
pred.dd$y <- with(pred.dd, reg.fun(x, par1, par2))

#draw the plot
p <- qplot(x,y, colour=g, data=dd)  #scatterplot of data
p + geom_line(data=pred.dd)         #add the curves of the functions 
流星番茄 2024-09-19 15:59:24

我和你有同样的问题。在一个非常不优雅的解决方案中,我发现的唯一解决方案是将统计函数组合在一起,如下所示:

p <- ggplot(temp, aes(Time, OD)) + geom_point(aes(colour = runID), size = 2) #works fine!

calc <- function(x){temp.n$M[ID] * (1 - exp(temp.n$D.1[ID] * temp.n$t0.1[ID] - x)))}
    p <- p +
      stat_function(fun = function(x){temp.n$M[1] * (1 - exp(temp.n$D.1[1] * temp.n$t0.1[1] - x)))) + 
      stat_function(fun = function(x){temp.n$M[2] * (1 - exp(temp.n$D.1[2] * temp.n$t0.1[2] - x)))) +
      stat_function(fun = function(x){temp.n$M[3] * (1 - exp(temp.n$D.1[3] * temp.n$t0.1[3] - x)))) +
      # etc

如果您只需要添加几行,那么这很好,但如果您有很多行,则不行。

I had the same problem with you. In a very non-elegant solution, the only solution I found was to hack the stat functions together something like this:

p <- ggplot(temp, aes(Time, OD)) + geom_point(aes(colour = runID), size = 2) #works fine!

calc <- function(x){temp.n$M[ID] * (1 - exp(temp.n$D.1[ID] * temp.n$t0.1[ID] - x)))}
    p <- p +
      stat_function(fun = function(x){temp.n$M[1] * (1 - exp(temp.n$D.1[1] * temp.n$t0.1[1] - x)))) + 
      stat_function(fun = function(x){temp.n$M[2] * (1 - exp(temp.n$D.1[2] * temp.n$t0.1[2] - x)))) +
      stat_function(fun = function(x){temp.n$M[3] * (1 - exp(temp.n$D.1[3] * temp.n$t0.1[3] - x)))) +
      # etc

Which is fine if you only have a few lines to add, but not if you have many.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文