After many google searches I decided to ask for your help, guys.
I am plotting just some observations at different time points and I want to add a linear regression with stat_smooth. However, I want the linear model with the intercept at 100 (because data are percentage relative to time 0). To do that, I found that the easiest way is to use the offset parameter in lm. The problem is how to get the number of 'y' observations per group(col and facet groups) to pass it to offset parameter.
If I use data with the same number of observations per group (10 in my case), I can just write the number and it works great:
myplot <- ggplot(mydt2, aes(x=Time_point, y=GFP_rel, col=Gene, fill=Gene,group=Gene))
myplot <- myplot + stat_smooth(method='lm', formula = y ~ x + 0, method.args=list(offset=rep(100,10))) +
You can use the
I(y - 100) coding in the formula as shown here instead of using an offset.
However, the predicted values for
stat_smooth will then be predictions for
y - 100, not
y. This line will go through 0. You can move the lines back to the position to display predictions of the original
y variable using
stat_smooth code would look something like
stat_smooth(method = "lm", formula = I(y - 100) ~ x + 0, position = position_nudge(y = 100))