Based on the example here Adding Regression Line Equation and R2 on graph, I am struggling to include the regression line equation for my model in each facet. However, I don't figure why is changing the limits of my x axis.
library(ggplot2)
library(reshape2)
df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6),
M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1))
df <- melt(df, id = c("year"))
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
stat_smooth_func(geom = 'text', method = 'lm', hjust = 0, parse = T) +
geom_smooth(method = 'lm', se = T) +
facet_wrap(~ variable) # as you can see, the scale_x_axis goes back to 1800
If I include on the x the limits,
scale_x_continuous(limits = c(1979,2010))
it does not show the regression coefficient anymore. What am I doing wrong here?
stat_smooth_func available here: https://gist.github.com/kdauria/524eade46135f6348140
You can use
stat_poly_eq
function from theggpmisc
package.Created on 2019-01-10 by the reprex package (v0.2.1.9000)
Probably someone will suggest a better solution, but as an alternative, you can change stat_smooth_func and you can make the final row like this
instead of
So, the plot will be like below