R/stat_fitted_resid.R

Defines functions stat_fitted_resid

Documented in stat_fitted_resid

#' @title stat_fitted_resid
#'
#' @description `ggplot2` layer for plotting a fitted vs. residual scatter plot.
#'
#' @param alpha Adjust transparency of points.
#' @param ... Currently ignored. For extendability.
#'
#' @return A `ggplot2` layer for plotting a fitted vs. residual scatter plot.
#' @examples
#' data(mtcars)
#' model <- lm(mpg ~ cyl + disp + hp, data = mtcars)
#' ggplot2::ggplot(data = model) + stat_fitted_resid()
#' @export
#' @importFrom rlang .data

stat_fitted_resid <- function(alpha = 0.5,
                              ...) {
  list(
    ggplot2::geom_point(mapping = ggplot2::aes(x = .data$.fitted,
                                               y = .data$.resid),
                        alpha = alpha),
    ggplot2::geom_hline(yintercept = 0,
                        linetype = "dashed"),
    ggplot2::labs(
      title = "Residuals vs Fitted",
      x = "Fitted values",
      y = "Residuals"
    )
    )
}

Try the gglm package in your browser

Any scripts or data that you put into this service are public.

gglm documentation built on March 31, 2023, 5:45 p.m.