banner



How To Add Regression Equation In R Ggplot2

In my early days as an annotator, adding regression line equations and R² to my plots in Microsoft Excel was a adept way to make an impression on the management. Because maths. In R, information technology is a piffling harder to attain. There are two main means to attain it: manually, and using the ggpubr library. In this web log post, I explain how to do it in both means.

First, let's get some dummy data from the mtcars information prepare, load necessary packages and remove scientific notation. Our starting time plot — without the equation — looks like this.

          library(ggplot2)  options(scipen=999) # no scientific notation  data(mtcars) df <- mtcars  ggplot(df,aes(x = wt, y = hp)) +    geom_point() +    geom_smooth(method = "lm", se=FALSE)        

The following solution was proposed 10 years ago in a Google Group and simply involved some base functions. I updated the solution a petty fleck and this is the resulting code. By passing the x and y variable to the eq part, the regression object gets stored in a variable. The coefficients and the R² are concatenated in a long cord.

          eq <- office(10,y) {   m <- lm(y ~ x)   equally.graphic symbol(     every bit.expression(       substitute(italic(y) == a + b %.% italic(10)*","~~italic(r)^ii~"="~r2,                 list(a = format(coef(g)[i], digits = 4),                 b = format(coef(m)[2], digits = 4),                 r2 = format(summary(m)$r.squared, digits = 3)))     )   ) }  ggplot(df,aes(x = wt, y = hp)) +    geom_point() +    geom_smooth(method = "lm", se=Faux) +   geom_text(x = 2, y = 300, label = eq(df$wt,df$hp), parse = TRUE)        

I haven't inverse the theme 1 little bit to keep the solution every bit uncomplicated as possible.

True, that's a lot of code for something that seems obvious for an Excel user. Using the ggpubr bundle, you lot can plot the regression and a broad range of measures. The eq.label and the rr.label are use respectively to access the regression line equation and the R².

          library(ggpubr) ggplot(df,aes(x = wt, y = hp)) +    geom_point() +    geom_smooth(method = "lm", se=Imitation) +   stat_regline_equation(label.y = 400, aes(label = ..eq.characterization..)) +   stat_regline_equation(label.y = 350, aes(label = ..rr.label..))        

Hither are the other measures you lot can access:

  • ..eq.characterization..: equation for the fitted polynomial every bit a character cord to exist parsed
  • ..rr.label..: R2 of the fitted model as a graphic symbol cord to be parsed
  • ..adj.rr.label..: Adjusted R2 of the fitted model as a character string to be parsed
  • ..AIC.labe..l: AIC for the fitted model.
  • ..BIC.characterization..: BIC for the fitted model.

By the way, you can easily use the measures from ggpubr in facets using facet_wrap() or facet_grid(). For every subset of your information, there is a different regression line equation and accompanying measures.

          ggplot(df,aes(x = wt, y = hp)) +    geom_point() +    geom_smooth(method = "lm", se=FALSE) +   stat_regline_equation(label.y = 400, aes(label = ..eq.label..)) +   stat_regline_equation(label.y = 350, aes(label = ..rr.label..)) +   facet_wrap(~vs)        

By the way, if y'all're having problem agreement some of the code and concepts, I can highly recommend "An Introduction to Statistical Learning: with Applications in R", which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Scientific discipline part, I can admittedly recommend this volume by Richard Cotton fiber. Hope it helps!

Congratulations, you tin now add the regression line equation and several measures to your ggplot2 visualizations.

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I brand mistakes too. If something is incorrect, incomplete or doesn't work, let me know in the comments beneath and aid thousands of visitors.

How To Add Regression Equation In R Ggplot2,

Source: https://www.roelpeters.be/how-to-add-a-regression-equation-and-r-squared-in-ggplot2/

Posted by: yatesevold1984.blogspot.com

0 Response to "How To Add Regression Equation In R Ggplot2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel