geom_lm: A Convenient 'ggplot2' Function for Linear Regression...

View source: R/geom_lm.R

geom_lmR Documentation

A Convenient ggplot2 Function for Linear Regression Plotting

Description

This function is an exact duplication of ggplot2::geom_smooth() with defaults more appropriate for statistics and econometrics. By default, geom_lm() plots with method = "lm" for a linear trend line and with se = FALSE because plotting standard errors around a linear trend confuses the viewer more than it demonstrates statistical uncertainty.

Usage

geom_lm(method = "lm", se = FALSE, formula = y ~ x, ...)

Arguments

method

Smoothing method (function) to use, accepts either NULL or a character vector, e.g. "lm", "glm", "gam", "loess" or a function, e.g. MASS::rlm or mgcv::gam, stats::lm, or stats::loess. "auto" is also accepted for backwards compatibility. It is equivalent to NULL.

For method = NULL the smoothing method is chosen based on the size of the largest group (across all panels). stats::loess() is used for less than 1,000 observations; otherwise mgcv::gam() is used with formula = y ~ s(x, bs = "cs") with method = "REML". Somewhat anecdotally, loess gives a better appearance, but is O(N^2) in memory, so does not work for larger datasets.

If you have fewer than 1,000 observations but want to use the same gam() model that method = NULL would use, then set method = "gam", formula = y ~ s(x, bs = "cs").

se

Display confidence interval around smooth? (TRUE by default, see level to control.)

formula

Formula to use in smoothing function, eg. y ~ x, y ~ poly(x, 2), y ~ log(x). NULL by default, in which case method = NULL implies formula = y ~ x when there are fewer than 1,000 observations and formula = y ~ s(x, bs = "cs") otherwise.

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

Examples

ggplot(mpg, aes(displ, hwy)) +
  ggplot2::geom_point() +
  geom_lm()

ldimartin0/ladtools documentation built on June 8, 2022, 3:30 p.m.