Add regression line equation and R^2 on graph

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Add regression line equation and R^2 on graph

šŸ“ Title: How to Add Regression Line Equation and R^2 on a ggplot Graph

šŸ‘‹ Hey there! Are you struggling with adding a regression line equation and R^2 on a ggplot graph? Don't worry, I've got you covered! In this blog post, I'll walk you through a step-by-step guide to achieving this. 😊

āœ… Problem: You want to visualize a regression line equation and R^2 on your ggplot graph but not sure how to do it.

ā­ļø Solution: Follow these simple steps to add the regression line equation and R^2 on your graph:

  1. First, make sure you have the ggplot2 library installed. If not, install it using the following code:

install.packages("ggplot2")
library(ggplot2)
  1. Now, let's create some dummy data for our example. Run the following code to generate a dataset:

df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
  1. We'll use the geom_smooth() function with the "lm" method to add a regression line to our plot. Additionally, we need to set se = FALSE to remove the confidence interval. Include the formula = y ~ x argument to specify the regression equation. We'll also add color = "black" to set the line color. Finally, don't forget to include geom_point() to add the data points. Run the following code:

p <- ggplot(data = df, aes(x = x, y = y)) +
            geom_smooth(method = "lm", se = FALSE, color = "black", formula = y ~ x) +
            geom_point()
  1. At this point, we have our basic graph. To add the regression line equation and R^2, we'll need to use the annotate() function. Add the following lines of code:

model <- lm(y ~ x, data = df)
eqn <- substitute(paste("Equation: ", y == a + b %.% x, ", R^2 = ", r2), 
                  list(a = format(coef(model)[1], digits = 2), 
                       b = format(coef(model)[2], digits = 2), 
                       r2 = format(summary(model)$r.squared, digits = 2)))

p + 
  annotate("text", x = max(df$x), y = max(df$y), label = as.character(as.expression(eqn)), 
           parse = TRUE, hjust = 1, vjust = 1, color = "red", fontface = "bold")
  1. Finally, run the code, and you'll have your ggplot graph with the regression line equation and R^2 displayed on it. šŸŽ‰

šŸ“£ So there you have it! You can now easily add a regression line equation and R^2 on your ggplot graphs using these simple steps. Hope this guide was helpful in solving your problem! If you have any further questions or ideas, feel free to share them in the comments below. Happy coding! šŸ˜„šŸ’»

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my