I would like to add two regression line equations and R2s with each facet. I adopted Jayden's function to solve the problem, but I found that each equation was the same. The likely reason is sending wrong subset of data to the function. Any advice will be appreciated!
My code:
p <- ggplot(data=df,aes(x=x))+
geom_point(aes(y = y1),size=2.0)+
geom_smooth(aes(y = y1),method=lm,se=FALSE,size=0.5,
fullrange = TRUE)+ # Add regression line;
annotate("text",x = 150,y =320, label = lm_eqn(lm(y1~x,df)), # maybe wrong
size = 2.0, parse = TRUE)+ # Add regression line equation;
geom_point(aes(y = y2),size=2.0)+
geom_smooth(aes(y = y2),method=lm,se=FALSE,size=0.5,
fullrange = TRUE)+ # Add regression line;
annotate("text",x = 225,y =50, label = lm_eqn(lm(y2~x,df)),
size = 2.0, parse = TRUE)+ # Add regression line equation;
facet_wrap(~trt)
My dataframe:
x y1 y2 trt
22.48349 34.2 31.0 6030
93.52976 98.5 96.0 6030
163.00984 164.2 169.8 6030
205.62072 216.7 210.0 6030
265.46812 271.8 258.5 6030
23.79859 35.8 24.2 6060
99.97307 119.4 90.6 6060
189.91814 200.8 189.3 6060
268.10060 279.5 264.6 6060
325.65609 325.7 325.4 6060
357.59726 353.6 353.8 6060
My plot:
PS. There are two lines and your equations in each facet, and the two lines are right, but the two equations are wrong. Obviously, upper/lower equations in the right and left facet should be different to each other.