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

时间:2019-06-08 17:16:48

标签: r ggplot2 regression non-linear-regression

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

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

![mean variation over time

使用以下代码:

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

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

以下代码未返回任何绘制的线(也没有错误):

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

没有这样做(found here):

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)),但是现在我有了另一个图例框,并且行始终使用相同的颜色...

what I want to do: change line and colors, merge legends

我也尝试在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"))

dotted line everywhere

1 个答案:

答案 0 :(得分:0)

感谢@PoGibas和this postgroup=group, color=group在ggplot的基础上,它给了我很好的结果。

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

这是最终的图形: final graph

NB:graphpad建议的拟合度(请参见第一个图形)比我最终选择的拟合度(LOESS)更stat_smooth(method = "lm", formula = y ~ x + I(x^2),size = 1, se = FALSE)