What is the best (easiest) approach to add neatly to a ggplot plot the regression equation, the R2, and the p-value (for the equation)? Ideally it should be
Here I use ggpmisc, with one call to stat_poly_eq() for the equation (centre top), and one call to stat_fit_glance() for the stats (pvalue and r2). The secret sauce for the alignment is using yhat as the left hand side for the equation, as the hat approximates the text height that then matches the superscript for the r2 - hat tip to Pedro Aphalo for the yhat, shown here.
Would be great to have them as one string, which means horizontal alignment would not be a problem, and then locating it conveniently in the plot space would be easier. I've raised as issues at ggpubr and ggpmisc.
I'll happily accept another better answer!
library(ggpmisc)
df_mtcars <- mtcars %>% mutate(factor_cyl = as.factor(cyl))
my_formula <- "y~x"
ggplot(df_mtcars, aes(x = wt, y = mpg, group = factor_cyl, colour= factor_cyl))+
geom_smooth(method="lm")+
geom_point()+
stat_poly_eq(formula = my_formula,
label.x = "centre",
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., sep = "~~~")),
parse = TRUE)+
stat_fit_glance(method = 'lm',
method.args = list(formula = my_formula),
#geom = 'text',
label.x = "right", #added to prevent overplotting
aes(label = paste("~italic(p) ==", round(..p.value.., digits = 3),
"~italic(R)^2 ==", round(..r.squared.., digits = 2),
sep = "~")),
parse=TRUE)+
theme_minimal()
Note facet also works neatly, and you could have different variables for the facet and grouping and everything still works.
Note: If you do use the same variable for group and for facet, adding label.y= Inf, to each call will force the label to the top of each facet (hat tip @dc37, in another answer to this question).