在图上添加方程到回归线

时间:2018-02-18 02:20:41

标签: r ggplot2

好的,所以我想在我的情节中添加回归线方程。 我发现this answer因某些原因对我不起作用。这就是我的数据:

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

但是我将以下输出作为一串文本而不做任何修改:

enter image description here

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

编辑:

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

1 个答案:

答案 0 :(得分:2)

根据the documentation$args = array( 'post_type' => 'product', 'meta_key' => '_featured', 'meta_value' => 'yes', 'posts_per_page' => 1 ); $featured_query = new WP_Query( $args ); if ($featured_query->have_posts()) : while ($featured_query->have_posts()) : $featured_query->the_post(); $product = get_product( $featured_query->post->ID ); // Output product information here endwhile; endif; wp_reset_query(); // Remember to reset parse参数是必需的,因此“标签将被解析为表达式并按照plotmath中的描述显示”。你已经省略了它,这应该是正确的调用:

geom_text()