Last active
October 29, 2017 21:56
-
-
Save angelovangel/dbb5c933d913a96c0f012d348dec6d81 to your computer and use it in GitHub Desktop.
plotting linear models plus the model coefficients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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