#' Scatterplot with linear regression line and formula
#'
#' This function allows you to plot a scatterplot using a formula interface with
#' the model-formula and R2 value on top
#' @param df data.frame, A data.frame that is used to plot the variables
#' @param frml formula, A formula object for plotting the variables in the form y ~ x
#' @param title string, Title of the resulting plot
#' @param method string, method that should be used to draw the fitting line
#' @param se boolean, should a confidence interval be drawn around the fitting line?
#' @param stat.frmle formula, the formula that is used to calculate the model-formula displayed on
#' the plot
#' @keywords plotting, linear model
#' @export
#' @examples
#' plot_linreg(df.plot,
#' frml = T_mean ~ elevation,
#' title = 'Scatterplot',
#' method = 'lm',
#' se = T,
#' stat.frml = y ~ x)
plot_linreg <- function(df, frml, title = '', method = 'lm', se = F, stat.frml = y~x) {
plot.frml <-
ggformula::gf_point(frml, data = df) +
ggplot2::geom_smooth(method = method, se = se) +
ggpmisc::stat_poly_eq(parse = T, formula = stat.frml,
aes(label = paste(stat(eq.label), stat(adj.rr.label), sep = "~~~~"))) +
ggplot2::ggtitle(title) +
ggplot2::theme_bw()
return(plot.frml)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.