Skip to content

Instantly share code, notes, and snippets.

@angelovangel
Last active October 29, 2017 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovangel/dbb5c933d913a96c0f012d348dec6d81 to your computer and use it in GitHub Desktop.
Save angelovangel/dbb5c933d913a96c0f012d348dec6d81 to your computer and use it in GitHub Desktop.
plotting linear models plus the model coefficients
# a general function for plotting linear models
# makes plots of the data and the model plus the model coefficients
# the function takes a linear model object as argument
ggplotLM <- function(fit) {
require(ggplot2)
require(broom)
ggplot(fit$model, aes_string(x = names(fit$model[2]),
y = names(fit$model[1]))
) +
geom_point() +
geom_smooth(
method = "lm",
color = "blue",
linetype = 5,
alpha = 0.5
) +
theme_bw() +
labs(title = paste("Linear model of",names(fit$model)[1], "~", names(fit$model)[2]),
subtitle = paste("R2adj. = ",signif(glance(fit)["adj.r.squared"], 5),
"Intercept =",signif(coef(fit) [[1]],5 ),
" Slope =",signif(coef(fit) [[2]], 5),
" P =",signif(glance(fit)["p.value"], 5),
"\nCall:", summary(fit)[1]),
caption = Sys.time())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment