2

Ok, so I want to add regression line equations to my plot. I found this answer which won't work for me for some reason. This is what my data looks like:

>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 

And I use a slightly modified version (I added y and x as input) of the function suggested in the awnsere:

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)); } 

I then plot my graph with:

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

But I get the following output as a string of text without modifications:

enter image description here

Any ideas why? It seems the expression function isn't working properly but I don't get why.

Edit:

Data.Inverse is the dataframe Plot_Data was melted (also with Substrate added using rep and mutate) from, and it looks like :

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 Answer 1

2

According to the documentation, the parse parameter to geom_text() is required so that "the labels will be parsed into expressions and displayed as described in plotmath." You've omitted it, this should be the correct call:

geom_text( x = 5, y = 0.5, label = lm_eqn(Data.Inverse$Substrate,Data.Inverse$X.Inh.P,Data.Inverse), color = "red", parse = TRUE ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.