user430997
user430997

Reputation: 317

Adding R-squared value to a ggplot?

I simulated some random data in R:

library(ggplot2)

set.seed(123)

n <- 100
x <- rnorm(n, mean = 50, sd = 10)
y <- 2 + 0.5 * x + rnorm(n, mean = 0, sd = 15)

sim_data <- data.frame(x = x, y = y)

I then made a scatterplot using ggplot and added a line of best fit:

ggplot(sim_data, aes(x = x, y = y)) +
    geom_point(color = "steelblue", alpha = 0.7, size = 3) +
    
    geom_smooth(method = "lm", color = "darkred", size = 1) +
    
    theme_minimal() +
    labs(
        title = "Scatterplot with Regression Line and Confidence Interval",
        x = "X Variable",
        y = "Y Variable"
    ) +
    
    theme(
        plot.title = element_text(face = "bold", size = 14),
        axis.title = element_text(face = "bold")
    )

enter image description here

I am wondering - in the ggplot code itself, is it possible to modify it so that I can add the r-squared value in the subtitle?

Upvotes: 0

Views: 44

Answers (1)

Edward
Edward

Reputation: 19339

The funky way would probably be to use the ggmisc package. If that is not possible, you can just go with a function:

sim_plot <- function(data) {
  R2 <- summary(lm(y~x, data=data))$r.squared
  rr.label <- paste("R^2 ==", sprintf("%.2f", R2))
  
  ggplot(data, aes(x = x, y = y)) +
    geom_point(color = "steelblue", alpha = 0.7, size = 3) +
    geom_smooth(method = "lm", color = "darkred", size = 1) +
    theme_minimal() +
    labs(
      title = "Scatterplot with Regression Line and Confidence Interval",
      subtitle = str2expression(rr.label),
      x = "X Variable",
      y = "Y Variable",
    ) 
}

sim_plot(sim_data)

enter image description here

Upvotes: 3

Related Questions