fit1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
ggplotRegression(fit1)
or even just
ggplotRegression(lm(Sepal.Length ~ Petal.Width, data = iris))
Disclaimer: This is not suitable for non-normal distributions or categorical variables!
This entry was posted in ggplot2, R. Bookmark the permalink.
Hi, This function has worked earlier but it seems some of the changes in the theming system in ggplot2 necessitate updating it. Can you please post the updated version? I am an R beginner and my trials in doing this have been unsuccessful.
Reference: https://github.com/wch/ggplot2/wiki/New-theme-system
Thanks a lot!
ggtitle(
So that the last element of the function reads:
ggtitle(paste(“Adj R2 = “,signif(summary(fit)$adj.r.squared, 5),
“; Intercept =”,signif(fit$coef[[1]],5 ),
“; Slope =”,signif(fit$coef[[2]], 5),
“; P =”,signif(summary(fit)$coef[2,4], 5)))
I was looking for method to obtain residuals and do other kind of regression using ggplot, which brought me here, I learned few things about regression. But my goal is still unfulfilled, you have not mentioned anywhere, how to find residual and plot residuals using ggplot without taking using ‘lm’ command. Would you throw some light on it. While going through your post meticulously, I found that there is no need to define a function and this line // ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) // is redudant, as you have used the fit$model in place of iris, (fit$model)[2] in place of iris$Sepal.Length ……In spite of going through a cumbersome function method, we can simply write // ggplot(iris,aes( Petal.Width,Sepal.Length))+geom_point()+geom_smooth(method=lm, col=’red’)// . Please correct me, if have missed something. Thank you
Sam, the function is plotting based on the model object, not the data itself, that is why aes_string and the model parameters are in there. I have outlined in the post already the code to plot with the data alone. This post is not for the residuals, merely visualisation of the regression itself. A quick Google of plotting residuals in ggplot2 gives plenty of hits, for example: https://rpubs.com/therimalaya/43190
Dear Susan,
thanks for illusteration, its very useful.
Can we plot a Multiple regression plot using the above code ? if not – can you please guide me how to do that.
Thanks.
Hi, nice post and thanks for the info.
I have problem to plot dose-response curves from a create model of class “drm” from a “drc” package using “ggplot”.
do you have any tips?
Thanks,
Danilo
This is great, thanks! It may be inappropriate statistically to use lm, but just for plotting categorical variables, I modified it thus:
ggplotRegression <- function (fit, jit=FALSE) {
require(ggplot2)
if(jit){
fit$model[,1] <- jitter(fit$model[,1])
fit$model[,2] <- jitter(fit$model[,2])
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
labs(title = paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 5),
"Intercept =",signif(fit$coef[[1]],5 ),
" Slope =",signif(fit$coef[[2]], 5),
" P =",signif(summary(fit)$coef[2,4], 5)))
What if you want to use your own defined fitted model which includes more than 1 input variable instead of the geom_smooth?
(i.e. myfit = lm(y ~ x + z + d + x:d, mydata)
Is there a patch or equivalent function that allows using “myfit” in geom_smooth instead of formula = …. ?
This is easy to understand and apply. Great illustration. Thanks.
What about if you have two different datasets that have differnt variables and you need all the variables to be in your regression, how would you do it?
I tired cbind function but when I run my regression, I had same names for all of the varialbles that are in my regrssion.
Thanks again
Hisham Alhawal
Hello, is there a way to change the size of the label title? I have tried to adjust by changing the number “5”, or by putting in a “size =” but have been unsuccessful. I am also having problems with including the “slope =”, where my console warning is that the dimensions “[2,4]” are incorrect. Here is my code:
KCCPIexportcorn %>% ggplot(aes(y = SIYieldNonirr, x = OM)) +
geom_point(size = 2, color = “purple4″) + geom_smooth(method=”lm”, se = FALSE, color = “black”, size = 1) +
labs(x = ‘OM Subrule’) + labs(y = expression(bold(paste(‘Dryland Corn Yield (kg ha’^-1,’)’))) ) +
labs(title = paste(“Adj R2 =”,signif(summary(OMsubrule)$adj.r.squared,5))) +
theme_classic() +
theme(
axis.title.x = element_text(color=”black”, size=8, face=”bold”),
axis.title.y = element_text(color=”black”, size=8, face=”bold”),
axis.text = element_text(color=”black”, size = 7, face=”bold”),
axis.line = element_line(color = “black”, size = 1))
Hi, nice post and thanks for the info.
I have problem to plot dose-response curves from a create model of class “drm” from a “drc” package using “ggplot”.
do you have any tips?
Thanks,
Dong-Mei