ggplot2 - Add regression line equation and R^2 on graph

Ggplot2 - Add regression line equation and R^2 on graph

In ggplot2, adding a regression line equation and R2 (coefficient of determination) on a graph requires a few steps involving data manipulation and annotation. Here's a detailed guide on how to achieve this:

Example Setup

Assume you have a dataset df with variables x and y, and you want to plot a scatter plot with a linear regression line.

# Example data set.seed(123) df <- data.frame(x = rnorm(100, mean = 10, sd = 2), y = rnorm(100, mean = 20, sd = 3)) # Plot using ggplot2 library(ggplot2) p <- ggplot(df, aes(x = x, y = y)) + geom_point() + labs(title = "Scatter plot with Regression Line", x = "X-axis", y = "Y-axis") 

Adding Regression Line and Equation

To add a regression line and its equation, you need to fit a linear model (lm) to your data and extract the equation and R2 value:

# Fit linear model lm_model <- lm(y ~ x, data = df) # Add regression line and equation p + geom_smooth(method = "lm", se = FALSE, color = "red") + annotate("text", x = min(df$x), y = max(df$y), label = paste("Y =", format(coef(lm_model)[1], digits = 2), "+", format(coef(lm_model)[2], digits = 2), "X")) + annotate("text", x = min(df$x), y = max(df$y) - 5, label = paste("R^2 =", format(summary(lm_model)$r.squared, digits = 3)), parse = TRUE, color = "red") 

Explanation

  • Fitting the Linear Model: lm_model <- lm(y ~ x, data = df) fits a linear regression model where y is the dependent variable and x is the independent variable using the lm function.

  • Adding Regression Line: geom_smooth(method = "lm", se = FALSE, color = "red") adds a linear regression line to the plot (se = FALSE suppresses the confidence interval shading).

  • Annotating Equation: annotate("text", ...) adds text annotations to the plot. The first annotate call adds the equation of the regression line using coefficients extracted from coef(lm_model). Adjust the position (x, y coordinates) and formatting (digits) as needed.

  • Annotating R2: The second annotate call adds the R2 value using summary(lm_model)$r.squared. parse = TRUE allows LaTeX-style formatting, enabling R2 symbol rendering.

Result

Here's the complete code to generate the plot:

# Example data set.seed(123) df <- data.frame(x = rnorm(100, mean = 10, sd = 2), y = rnorm(100, mean = 20, sd = 3)) # Plot using ggplot2 library(ggplot2) p <- ggplot(df, aes(x = x, y = y)) + geom_point() + labs(title = "Scatter plot with Regression Line", x = "X-axis", y = "Y-axis") # Fit linear model lm_model <- lm(y ~ x, data = df) # Add regression line and equation p + geom_smooth(method = "lm", se = FALSE, color = "red") + annotate("text", x = min(df$x), y = max(df$y), label = paste("Y =", format(coef(lm_model)[1], digits = 2), "+", format(coef(lm_model)[2], digits = 2), "X")) + annotate("text", x = min(df$x), y = max(df$y) - 5, label = paste("R^2 =", format(summary(lm_model)$r.squared, digits = 3)), parse = TRUE, color = "red") 

This script generates a scatter plot with a linear regression line fitted to the data, displaying the regression equation and R2 value on the plot. Adjust the plot aesthetics and annotations according to your specific requirements.

Examples

  1. How to add a linear regression line and its equation to a ggplot2 scatter plot in R?

    • Description: Visualize a linear regression model with the regression line equation displayed on a ggplot2 scatter plot.
    • Code:
      library(ggplot2) library(dplyr) # Example data df <- data.frame(x = 1:20, y = 2*(1:20) + rnorm(20)) # Create ggplot with scatter plot and regression line ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm", se = FALSE) + # Add linear regression line stat_regline_equation(label.x = 0.8, label.y = 0.9) # Display equation 
  2. How to annotate a ggplot2 plot with regression equation and R-squared value?

    • Description: Annotate a ggplot2 plot with the equation of the regression line and the R-squared value.
    • Code:
      library(ggplot2) library(dplyr) # Example data df <- data.frame(x = 1:20, y = 2*(1:20) + rnorm(20)) # Perform linear regression lm_model <- lm(y ~ x, data = df) # Extract coefficients and R-squared coef <- coef(lm_model) rsq <- summary(lm_model)$r.squared # Create ggplot with scatter plot and regression line p <- ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm", se = FALSE) # Add linear regression line # Annotate with equation and R-squared p + annotate("text", x = max(df$x), y = max(df$y), label = paste("y = ", round(coef[2], 2), "x + ", round(coef[1], 2), "\nR^2 = ", round(rsq, 3))) 
  3. How to display regression line equation and R-squared on a facetted ggplot2 plot in R?

    • Description: Add regression line equation and R-squared value on each facet of a facetted ggplot2 plot.
    • Code:
      library(ggplot2) library(dplyr) # Example data with facets df <- data.frame(x = rep(1:10, 4), y = 2*(rep(1:10, 4)) + rnorm(40), facet = rep(c("A", "B"), each = 20)) # Perform linear regression by facet lm_model <- df %>% group_by(facet) %>% do(model = lm(y ~ x, data = .)) # Extract coefficients and R-squared coef_df <- lm_model %>% summarise(intercept = coef(model)[1], slope = coef(model)[2], rsq = summary(model)$r.squared) # Create ggplot with facets and regression lines p <- ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm", se = FALSE) + # Add linear regression line facet_wrap(~ facet) # Annotate with equation and R-squared on each facet p + geom_text(data = coef_df, aes(x = Inf, y = Inf, label = paste("y = ", round(slope, 2), "x + ", round(intercept, 2), "\nR^2 = ", round(rsq, 3))), hjust = 1, vjust = 1, size = 3, parse = TRUE) 
  4. How to add regression line equation and R-squared to a scatter plot using ggplot2 and ggpmisc in R?

    • Description: Utilize ggpmisc package to add regression line equation and R-squared value to a ggplot2 scatter plot.
    • Code:
      library(ggplot2) library(ggpmisc) library(dplyr) # Example data df <- data.frame(x = 1:20, y = 2*(1:20) + rnorm(20)) # Create ggplot with scatter plot and regression line ggplot(df, aes(x = x, y = y)) + geom_point() + stat_smooth(method = "lm", se = FALSE) + # Add linear regression line stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~~")), formula = y ~ x, parse = TRUE) # Display equation and R-squared 
  5. How to customize the appearance of regression line equation and R-squared annotation in ggplot2?

    • Description: Customize fonts, colors, and positions of regression line equation and R-squared annotation on a ggplot2 plot.
    • Code:
      library(ggplot2) library(dplyr) # Example data df <- data.frame(x = 1:20, y = 2*(1:20) + rnorm(20)) # Perform linear regression lm_model <- lm(y ~ x, data = df) coef <- coef(lm_model) rsq <- summary(lm_model)$r.squared # Create ggplot with scatter plot and regression line p <- ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm", se = FALSE) # Add linear regression line # Annotate with customized equation and R-squared p + annotate("text", x = max(df$x), y = max(df$y), label = paste("y = ", round(coef[2], 2), "x + ", round(coef[1], 2), "\nR^2 = ", round(rsq, 3)), color = "blue", size = 5, hjust = 1, vjust = -1) 
  6. How to add a polynomial regression line and its equation to a ggplot2 plot in R?

    • Description: Visualize a polynomial regression model with the equation displayed on a ggplot2 plot.
    • Code:
      library(ggplot2) library(dplyr) # Example data df <- data.frame(x = 1:20, y = 0.5*(1:20)^2 + rnorm(20)) # Create ggplot with scatter plot and polynomial regression line ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE) + # Add polynomial regression line stat_poly_eq(formula = y ~ poly(x, 2), aes(label = paste(..eq.label.., sep = "~~~~")), parse = TRUE) # Display equation 
  7. How to add a robust regression line and its equation to a ggplot2 plot in R?

    • Description: Add a robust regression model (e.g., using Huber's M-estimator) with the equation displayed on a ggplot2 plot.
    • Code:
      library(ggplot2) library(dplyr) library(MASS) # For robust regression # Example data df <- data.frame(x = 1:20, y = 2*(1:20) + rnorm(20)) # Perform robust regression using MASS package lm_model <- rlm(y ~ x, data = df) # Create ggplot with scatter plot and robust regression line ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "rlm", se = FALSE) + # Add robust regression line stat_poly_eq(formula = y ~ x, aes(label = paste(..eq.label.., sep = "~~~~")), parse = TRUE) # Display equation 
  8. How to add a loess regression line and its equation to a ggplot2 plot in R?

    • Description: Incorporate a loess (locally weighted scatterplot smoothing) regression model with its equation displayed on a ggplot2 plot.
    • Code:
      library(ggplot2) library(dplyr) # Example data df <- data.frame(x = 1:20, y = sin(1:20) + rnorm(20)) # Create ggplot with scatter plot and loess regression line ggplot(df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "loess", se = FALSE) + # Add loess regression line stat_poly_eq(formula = y ~ x, aes(label = paste(..eq.label.., sep = "~~~~")), parse = TRUE) # Display equation 

More Tags

python-importlib ksqldb aio-write fopen editing launcher sanitization numeric-input android-backup-service coin-flipping

More Programming Questions

More Weather Calculators

More Housing Building Calculators

More Mortgage and Real Estate Calculators

More Electrochemistry Calculators