I wonder how to add regression line equation and R^2 on the ggplot
. My code is
library(ggplot2)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
p
Any help will be highly appreciated.
Here is one solution
EDIT. I figured out the source from where I picked this code. Here is the link to the original post in the ggplot2 google groups
I've modified Ramnath's post to a) make more generic so it accepts a linear model as a parameter rather than the data frame and b) displays negatives more appropriately.
Usage would change to:
I changed a few lines of the source of
stat_smooth
and related functions to make a new function that adds the fit equation and R squared value. This will work on facet plots too!I used the code in @Ramnath's answer to format the equation. The
stat_smooth_func
function isn't very robust, but it shouldn't be hard to play around with it.https://gist.github.com/kdauria/524eade46135f6348140. Try updating
ggplot2
if you get an error.I included a statistics
stat_poly_eq()
in my packageggpmisc
that allows this answer:This statistic works with any polynomial with no missing terms, and hopefully has enough flexibility to be generally useful. The R^2 or adjusted R^2 labels can be used with any model formula fitted with lm(). Being a ggplot statistic it behaves as expected both with groups and facets.
The 'ggpmisc' package is available through CRAN.
Version 0.2.6 was just accepted to CRAN.
It addresses comments by @shabbychef and @MYaseen208.
@MYaseen208 this shows how to add a hat.
@shabbychef Now it is possible to match the variables in the equation to those used for the axis-labels. To replace the x with say z and y with h one would use:
Being these normal R parsed expressions greek letters can now also be used both in the lhs and rhs of the equation.
[2017-03-08] @elarry Edit to more precisely address the original question, showing how to add a comma between the equation- and R2-labels.
really love @Ramnath solution. To allow use to customize the regression formula (instead of fixed as y and x as literal variable names), and added the p-value into the printout as well (as @Jerry T commented), here is the mod:
Unfortunately, this doesn't work with facet_wrap or facet_grid.