r - 有没有办法在一张图上绘制具有不同衰减常数的指数衰减曲线?

标签 r graphics nls

我一直在尝试将不同的指数衰减曲线绘制到一张图上。最初我认为这会很容易,但事实证明这很令人沮丧。

我想得到的:

Picture of what I am trying to get.

nlsplot(k_data_nls, model = 6, start = c(a= 603.3, b= -0.03812), xlab = "hours", ylab = "copies")

nlsplot(r4, model=6, start=c(a=25.5487,b=-0.5723), xlab = "hours", ylab = "copies")

这里是数据的一些附加代码:

df4 <- data.frame(hours=c(0,1,3,5,12,24,48,96,168,336,504,720), copies=c(603.3,406,588,393.27,458.47,501.67,767.53,444.13,340.6,298.47,61.42,51.6))
nlsfit(df4, model=6, start=c(a=603.3,b=-0.009955831526))
d4plot <- nlsplot(df4, model=6, start=c(a=603.3,b=-0.009955831526))

r4 <- data.frame(hours=c(0,1,3,5,12,24,48,96,168,336,504,720), copies=c(26,13.44,4.57,3.12,6.89,0.71,0.47,0.47,0,0,0.24,0.48))
nlsLM(copies ~ a*exp(b*hours), data=r4, start=list(a=26,b=-0.65986))
r4plot <- nlsplot(r4, model=6, start=c(a=25.5487,b=-0.5723))

本质上,我希望能够在一张图表上同时绘制这两个图。我是 R 的新手,所以我不太确定从这里可以去哪里。谢谢!

最佳答案

我不知道这是否真的有用,因为它太具体了,但这就是我要做的(使用 ggplot2)。首先,您需要要绘制的函数的数据。为您要显示的所有值取 x,并将带有系数的函数应用于数据。您需要有数据点,而不仅仅是一个函数来绘制数据。

df_simulated <- data.frame("x" = rep(1:100, 2),
                           "class"= rep(c("DNA", "RNA"), each = 100))
df_simulated$y <- c(1683.7 * exp(-0.103 * 1:100),  # DNA
                    578.7455 * exp(-0.156 * 1:100))  # RNA

但是,由于我从未使用过您使用的包,我不知道如何从模型中提取值,所以我在您的示例图中采用了这些值。重要的是,两个组的“模拟”值都在一个数据框中,并且您有一个列将这些点归因于各自的组(RNA 或 DNA)。至少如果你这样做会更容易。然后你需要一个数据框,其中包含你对这些点的实际观察。我又发明了数据:

df_observed <- data.frame("x" = c(12, 13, 25, 26, 50, 51),
                          "y" = c(500, 50, 250, 25, 0, 5),
                          "class" = rep(c("DNA", "RNA"), 3))

然后你就可以创建情节了。使用 color=class,您指定数据点将按“类”分组并相应地着色。 (“苹果”和“香蕉”只是用来演示换行符的虚拟词)

ggplot() +
  geom_line(data = df_simulated, aes(x = x, y = y, color = class), size = 1, linetype = "dashed") +
  geom_point(data = df_observed, aes(x = x, y = y, color = class), size = 4, pch = 1) +
  annotate("text", x = 50, y = 1250, label = "DNA\napple", color = "tomato", hjust = 0) +
  annotate("text", x = 50, y = 750, label  ="RNA\nbanana", color = "steelblue", hjust = 0) +
  ggtitle(expression(~italic("Styela clava")~"(isolated)")) +
  ylab("COI copies per 1ml") +
  xlab("Time since removal of organisms (hours)") +
  theme_classic() +
  theme(legend.position = "none") +
  scale_color_manual(values = c("DNA" = "tomato", "RNA" = "steelblue"))

这是输出:

enter image description here

关于r - 有没有办法在一张图上绘制具有不同衰减常数的指数衰减曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68605877/

相关文章:

r - 通过跨多列测试逻辑条件进行过滤

c - 制作一个简单的视频游戏

r - 将 nls() 函数应用于多个子集

r - 使用 R、Sweave 和 LaTeX 创建出版物质量表的一般指南

通过链接读取文件

r - 在 R 中查询 PostgreSQL 数据库时如何修复警告消息 "Closing open result set, cancelling previous query"?

Java 无法使 TempListener 工作

c++ - 如何在 SDL_surface 中设置像素?

R:如何为要通过 lm 或 nls 估计的模型添加约束?

c - 在 Win32 中使用 C 语言中的扩展 ASCII 归类表