add_lm_line: Add the best line with (equation, R-squared, p-value) or (fit...

View source: R/add_lm_line.R

add_lm_lineR Documentation

Add the best line with (equation, R-squared, p-value) or (fit table, R-squared) to scatterplot.

Description

add_lm_line takes a ggplot object (scatterplot) as input and adds the best line to the plot with its equation, the R-squared value, and the p-value of the x term coef. estimate. (Note: because it's simple linear regression, the p-value is the same as the p-value obtained when running correlation test, and the R-squared value is just the correlation squared.) Alternatively, by settting show = "tb", user can add to the plot the entire fit table (term, coef. estimate, standard error, tstat and pval) and the R-squared value. If the input scatterplot object has 1+ colored points, it will add 1+ lines with each line corresponding to a color. When this happens, use the default show = "eq" since printing 1+ fit tables doesn't work.

Usage

add_lm_line(
  p,
  alpha = 0.8,
  linew = 1,
  show = "eq",
  eq_tb_xpos = "left",
  eq_tb_ypos = "top",
  pv_r2_xpos = "right",
  pv_r2_ypos = "bottom",
  ...
)

Arguments

p

A ggplot object, must be a scatter plot.

alpha

A number between 0 and 1, transparency level of the best line. Smaller value means more transparent. Default = 0.8.

linew

Number, width of the line. Default = 1.

show

String, "eq" or "tb". If "eq" (default), add the equation of the best fit line, the R-squared value, and the p-value of the x term coef. estimate to the plot. If "tb", add the fit table and the R-squared value to the plot.

eq_tb_xpos

Numeric between 0 and 1 or character. x-position of the equation or fit table label. Default = "left".

eq_tb_ypos

Numeric between 0 and 1 or character. y-position of the equation or fit table label. Default = "top".

pv_r2_xpos

Numeric between 0 and 1 or character. x-position of the p-value or R-squared value label. Default = "right".

pv_r2_ypos

Numeric between 0 and 1 or character. y-position of the p-value or R-squared value label. Default = "bottom".

...

Other arguments for ggpmisc::stat_poly_eq() or ggpmisc::stat_fit_tb(). For example, for ggpmisc::stat_poly_eq(), coef.digits sets the number of digits for coef. estimates, rr.digits sets the number of digits for R-squared value. For ggpmisc::stat_fit_tb(), digits sets the digits for all numbers in the fit table.

Value

A ggplot scatterplot object with the best line added with detailed labels.

Examples

inst/examples/ex-add_lm_line.R

gmlang/ezplot documentation built on Sept. 18, 2022, 6:33 a.m.