R 在同一图表上绘制两个(或更多)回归方程

R 在同一图表上绘制两个(或更多)回归方程,r,ggplot2,regression,linear-equation,R,Ggplot2,Regression,Linear Equation,我需要在同一张图上显示两个线性回归方程和系数(r,r²,p,N)。我使用facet_网格进行了此操作,但现在这两条曲线无法单独显示 我修改了类似于facet_网格函数的代码: equation = function(file) { mod = lm(y ~ x,data=file) mod_sum = summary(mod) formula = sprintf("y= %.3f %+.3f*x", coef(mod)[1], coef(mod)[2]) r = mod_su

我需要在同一张图上显示两个线性回归方程和系数(r,r²,p,N)。我使用facet_网格进行了此操作,但现在这两条曲线无法单独显示

我修改了类似于facet_网格函数的代码:

  equation = function(file) {
  mod = lm(y ~ x,data=file)
  mod_sum = summary(mod)
  formula = sprintf("y= %.3f %+.3f*x", coef(mod)[1], coef(mod)[2])
  r = mod_sum$r.squared
  r2 = sprintf("r2= %.3f", r)
  x  = cor.test(~x + y,data=file)
  r0 = sprintf("r= %.3f", x[4])
  p1 = pf(mod_sum$fstatistic[1],mod_sum$fstatistic[2],mod_sum$fstatistic[3],lower.tail=F)
 p =sprintf("p = %.3f", p1)
n0 = length(mod_sum$residual)
n1 = sprintf("N = %.f", n0)
data.frame(formula=formula, r=r0,r2=r2, p=p,n=n1, stringsAsFactors=FALSE)
}

equation_end = ddply(file, c("outlier"), equation) 
两个回归的数据在同一列中,并由因子“离群值”分隔


如何在同一图表上显示这些方程式?

您可以使用
注释
在图形上放置文本

library(ggplot2)
ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  annotate("text", c(-1,-1), c(3,4), label=equation_end$formula)

如果希望文本与某些行的颜色相同,请尝试使用
geom_text

ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  geom_smooth(fill=NA) +
  geom_text(data=equation_end, aes(x=c(-1,-1), y=c(3,4), label=formula), show_guide=F)
数据:

库(plyr)

x您可以使用
注释
在图形上放置文本

library(ggplot2)
ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  annotate("text", c(-1,-1), c(3,4), label=equation_end$formula)

如果希望文本与某些行的颜色相同,请尝试使用
geom_text

ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  geom_smooth(fill=NA) +
  geom_text(data=equation_end, aes(x=c(-1,-1), y=c(3,4), label=formula), show_guide=F)
数据:

库(plyr)

x您可以使用
注释
在图形上放置文本

library(ggplot2)
ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  annotate("text", c(-1,-1), c(3,4), label=equation_end$formula)

如果希望文本与某些行的颜色相同,请尝试使用
geom_text

ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  geom_smooth(fill=NA) +
  geom_text(data=equation_end, aes(x=c(-1,-1), y=c(3,4), label=formula), show_guide=F)
数据:

库(plyr)

x您可以使用
注释
在图形上放置文本

library(ggplot2)
ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  annotate("text", c(-1,-1), c(3,4), label=equation_end$formula)

如果希望文本与某些行的颜色相同,请尝试使用
geom_text

ggplot(file, aes(x, y, color=outlier)) +
  geom_point() +
  geom_smooth(fill=NA) +
  geom_text(data=equation_end, aes(x=c(-1,-1), y=c(3,4), label=formula), show_guide=F)
数据:

库(plyr)

你能举个例子吗?类似于
dput(等式_end)
的东西,或者如果不是太大的话会有很大帮助……你能举个例子吗?类似于
dput(等式_end)
的东西,或者如果不是太大的话会有很大帮助……你能举个例子吗?类似于
dput(等式_end)
的东西,或者如果不是太大的话会有很大帮助……你能举个例子吗?类似于
dput(等式_end)
的东西,或者如果它不是太大的话,会有很大帮助……按照你的方式。我使用粘贴来匹配系数。然而,这有点让人困惑,需要和线条的颜色联系起来才能获得好的效果。也许是使用geom_文本。但还是不起作用我不确定你的意思,你想让文字的颜色和线条一样吗?没错。因为这样我知道它指的是哪一组数据。当我使用geom_文本时,它只能显示其中一个等式谢谢,这样看起来它工作了!我只是按照你的方式定位等式。我使用粘贴来匹配系数。然而,这有点让人困惑,需要和线条的颜色联系起来才能获得好的效果。也许是使用geom_文本。但还是不起作用我不确定你的意思,你想让文字的颜色和线条一样吗?没错。因为这样我知道它指的是哪一组数据。当我使用geom_文本时,它只能显示其中一个等式谢谢,这样看起来它工作了!我只是按照你的方式定位等式。我使用粘贴来匹配系数。然而,这有点让人困惑,需要和线条的颜色联系起来才能获得好的效果。也许是使用geom_文本。但还是不起作用我不确定你的意思,你想让文字的颜色和线条一样吗?没错。因为这样我知道它指的是哪一组数据。当我使用geom_文本时,它只能显示其中一个等式谢谢,这样看起来它工作了!我只是按照你的方式定位等式。我使用粘贴来匹配系数。然而,这有点让人困惑,需要和线条的颜色联系起来才能获得好的效果。也许是使用geom_文本。但还是不起作用我不确定你的意思,你想让文字的颜色和线条一样吗?没错。因为这样我知道它指的是哪一组数据。当我使用geom_文本时,它只能显示其中一个等式谢谢,这样看起来它工作了!我只是在定位方程式