我一直试图在一张图上绘制不同的指数衰减曲线。最初我认为这会很容易,但结果却令人沮丧。
我想得到什么:

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")
Run Code Online (Sandbox Code Playgroud)
以下是数据的一些附加代码:
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))
Run Code Online (Sandbox Code Playgroud)
本质上,我希望能够在一张图上获得这两个图。我是 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
Run Code Online (Sandbox Code Playgroud)
但是,由于我从未使用过您使用的软件包,因此我不知道如何从模型中提取值,因此我采用了您的示例图中的值。重要的是,两组的“模拟”值都在一个数据框中,并且您有一列将点归因于相应的组(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))
Run Code Online (Sandbox Code Playgroud)
然后你可以创建情节。与color=class你指定的数据点将由“类”进行分组,将相应的颜色。(“apple”和“banana”只是用来演示换行的虚拟词)
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"))
Run Code Online (Sandbox Code Playgroud)
这是输出: