繁体   English   中英

将方程添加到绘图上的回归线

[英]Add equation to regression line on plot

好的,所以我想在我的图中添加回归线方程。 我发现这个答案出于某种原因对我不起作用。 这是我的数据的样子:

>Plot_Data
   Treatment      value Substrate
1    Control 0.16666667  10.00000
2    Control 0.03333333   2.00000
3    Control 0.02380952   1.00000
4    Control 0.01388889   0.50000
5    Control 0.01250000   0.25000
6    Control 0.01219512   0.12500
7    Control 0.01176471   0.03125
8     +Inh P 0.50000000  10.00000
9     +Inh P 0.14285714   2.00000
10    +Inh P 0.10000000   1.00000
11    +Inh P 0.08333333   0.50000
12    +Inh P 0.07142857   0.25000
13    +Inh P 0.06666667   0.12500
14    +Inh P 0.06250000   0.03125
15    +Inh Q 0.43103448  10.00000
16    +Inh Q 0.08403361   2.00000
17    +Inh Q 0.05494505   1.00000
18    +Inh Q 0.02610966   0.50000
19    +Inh Q 0.02000000   0.25000
20    +Inh Q 0.01470588   0.12500
21    +Inh Q 0.01265823   0.03125

我使用了 awnsere 中建议的函数的稍微修改版本(我添加了 y 和 x 作为输入):

lm_eqn <- function(y,x,df){
    m <- lm(y ~ x, df);
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                     list(a = format(coef(m)[1], digits = 2), 
                          b = format(coef(m)[2], digits = 2), 
                          r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq));
}

然后我绘制我的图表:

Plot <- ggplot(Plot_Data,aes(x=Substrate,y=value,group=Treatment,color=Treatment))+
geom_point(shape=1)+
geom_smooth(method = lm,fullrange =T,se=F,size=0.75)+
xlab(expression("[S]"^-1))+
ylab(expression("V"[0]^-1))+
xlim(c(-1.5,10))+
ggtitle("Adenylate Kinase rate graph")+
theme(axis.title = element_text(size=12),
      plot.title = element_text(hjust = 0.5))+
geom_text(x=5,y=0.5,
label = lm_eqn(Data.Inverse$Substrate,Data.Inverse$X.Inh.P,Data.Inverse),color = "red")

但是我得到以下输出作为文本字符串而没有修改:

在此处输入图片说明

任何想法为什么? 似乎表达式函数不能正常工作,但我不明白为什么。

编辑:

Data.Inverse是数据帧Plot_Datamelt (也使用repmutate添加了 Substrate ),它看起来像:

Substrate    Control    X.Inh.P    X.Inh.Q
1  10.00000 0.16666667 0.50000000 0.43103448
2   2.00000 0.03333333 0.14285714 0.08403361
3   1.00000 0.02380952 0.10000000 0.05494505
4   0.50000 0.01388889 0.08333333 0.02610966
5   0.25000 0.01250000 0.07142857 0.02000000
6   0.12500 0.01219512 0.06666667 0.01470588
7   0.03125 0.01176471 0.06250000 0.01265823

根据文档,需要geom_text()parse参数,以便“标签将被解析为表达式并按照 plotmath 中的描述显示。” 你已经省略了它,这应该是正确的调用:

geom_text(
    x = 5,
    y = 0.5,
    label = lm_eqn(Data.Inverse$Substrate,Data.Inverse$X.Inh.P,Data.Inverse),
    color = "red",
    parse = TRUE
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM