I have to create a large (100+) number of ggplots of linear models. I would like to add the p-value (and potentially R2) to each plot. I know it is possible to do this using
ggpmisc. Here, I employ
stat_fit_glance to add the p-value. My 'problem' is that both of these require me to run
lm first to be inserted as formula = my_lm.
As I have to create a large number of plots, I was wondering if there is a way to avoid creating the lm object first, and simply have it calculated while producing the ggplot? I can do it for t-tests for boxplots using
stat_compare_means, and really hope to find a way to do it with lm's as well.
My code is present below. I would like to be able to skip the first line of code:
my_lm <- lm(y ~ x) ggplot(data = complete, aes(x= x, y = y))+ geom_point()+ theme_classic()+ geom_smooth(method = "lm")+ labs(x="Ellenberg F", y = "Species richness")+ stat_fit_glance(method = 'lm', method.args = list(data = complete, formula = my_lm), geom = 'text', aes(label = paste("p-value = ", signif(..p.value.., digits = 4), sep = "")), label.x = 8.5, label.y = 25, size = 3)
I have tried simply putting formula = y ~ x with no luck.