如何用ggplot2绘制三种不同的非线性回归

How to draw three differents non-linear regression with ggplot2

我正在尝试使用 ggplot2 绘制三种不同的非线性回归(就像我在下面使用图形板(虚线)所做的那样(因为图形板无法比较组之间的非线性回归):

到目前为止,我画了这张图:

使用以下代码:

gp <- ggplot(datapoidsmono, aes(x = time, y = weight)) +
  stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
  stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) + 
  scale_x_discrete(name = "Days after injection") +
  scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
  scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
  scale_shape_manual(values=c(15,16,17), name  ="Treatment", labels=c("A", "B", "C")) +
  ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position = "right") + 
  theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white")) +
  theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank())

我不知道如何为每个组绘制非线性回归。

下面的代码没有return任何画线(也没有错误):

ggplot(datapoidsmono, aes(time, weight, color = group)) +
  geom_point() +
  stat_smooth(method = "lm", se=FALSE)

这个也没有 ():

ggplot(datapoidsmono, aes(x = time, y = weight, colour=group)) +
  stat_smooth(method = 'nls', formula = 'y~a*exp(b*x)') +
  stat_smooth(color = 1, method = 'nls', formula = 'y~a*exp(b*x)') +
  geom_point(aes(fill=group))

任何想法或线索都将有助于继续!谢谢


更新

根据@PoGibas 的建议,我在第一行内的 aes 中添加了 "group=group",这完美地绘制了线条!

我尝试了多种解决方案以获得完美契合:

gp + ggplot(aes(group=group))
  stat_smooth(method = "lm", formula = y ~ x, size = 1, se = FALSE,colour = "black") + 
  stat_smooth(method = "lm", formula = y ~ x + I(x^2),size = 1, se = FALSE, colour = "blue") + 
  stat_smooth(method = "loess", formula = y ~ x, size = 1, se = FALSE, colour = "red") + 
  stat_smooth(method = "gam", formula = y ~ s(x), size = 1, se = FALSE, colour = "green") + 
  stat_smooth(method = "gam", formula = y ~ s(x, k = 3), size = 1, se = FALSE, colour = "violet") +
  stat_smooth(method = "auto", se=F, colour = "yellow")

但我认为 gp + stat_smooth() 就完美地完成了工作(使用了 LOESS 方法)。

现在我正在尝试改变外观(虚线)和适合的颜色...

我试过了gp + stat_smooth(se=F, aes(fill = group)),但现在我有另一个图例框,而且我的线条总是有相同的颜色...

我也尝试在 aes 中添加 linetype=group,但是当我使用 scale_linetype_manual(values=c("dotted", "dotted", "dotted")) 时,每一行都是虚线(包括错误栏)

完整代码为:

ggplot(datapoidsmono, aes(x = time, y = weight, group=group, linetype=group)) +
  stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
  stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) + 
  scale_x_discrete(name = "Days after injection") +
  scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
  scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
  scale_shape_manual(values=c(15,16,17), name  ="Treatment", labels=c("A", "B", "C")) +
  ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position = "right") + 
  theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white")) +
  theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
  stat_smooth(se=F, aes(fill = group)) +
  scale_linetype_manual(values=c("dotted", "dotted", "dotted"))

感谢@PoGibas 和 ,我添加了 group=group, color=group 在 ggplot 的 aes 中,它给了我一个很好的结果。

ggplot(datapoidsmono, aes(x = time, y = weight, group=group, color=group)) +
  stat_summary(aes(color = group), fun.data="mean_sdl", fun.args = list(mult=1), geom="errorbar", position = "identity", size=0.5, width=0.2) +
  stat_summary(fun.y = "mean", geom = "point", size=3, aes(shape=group,color=group)) + 
  scale_x_discrete(name = "Days after injection") +
  scale_y_continuous(name = "Weight (g)", limits=c(0, 4000), breaks = seq(0, 4000,500)) +
  scale_color_manual(values=c("green", "blue", "red"), name="Treatment", labels=c("A","B","C")) +
  scale_shape_manual(values=c(15,16,17), name  ="Treatment", labels=c("A", "B", "C")) +
  ggtitle("Weight variation over time") + theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position = "right") + 
  theme(legend.background = element_rect(size=0.5, linetype="solid", color ="black", fill="white")) +
  theme(axis.line.x = element_line(size = 0.5, color = "black"),axis.text.x = element_text(color="black", size = 12),axis.line.y = element_line(size = 0.5, color = "black"),axis.text.y = element_text(color="black", size = 12),axis.title = element_text(size =15, face="bold"),plot.title = element_text(size =20, face = "bold"),panel.grid.major = element_line(color = "#F1F1F1"),panel.grid.minor = element_blank(), panel.background = element_blank()) +
  stat_smooth(se=F, linetype="dotted")

这是最终图表:

注意:graphpad 提出的拟合度(见第一张图)比我最终选择的拟合度 (LOESS)

stat_smooth(method = "lm", formula = y ~ x + I(x^2),size = 1, se = FALSE)