1

When I run the following code in rstudio (see below), everything looks great except for the linear regression line equation. Instead of getting y = 3.142x -4.751, I get y = c(3.142)xc(-4.751).

What can I do to fix this? Thank you very much in advance.

set.seed(3L)
library(ggplot2)

df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
lm_eqn <- function(df){

# browser()
m <- lm(y ~ x, df)
a <- coef(m)[1]
a <- ifelse(sign(a) >= 0, 
     paste0(" + ", format(a, digits = 4)), 
     paste0(" - ", format(-a, digits = 4))  )

eq1 <- substitute( paste( italic(y) == b, italic(x), a ),
     list(a = a, 
          b = format(coef(m)[2], digits=4)))

eq2 <- substitute( paste( italic(R)^2 == r2 ), 
     list(r2 = format(summary(m)$r.squared, digits = 3)))

c( as.character(as.expression(eq1)), as.character(as.expression(eq2)))
}

labels <- lm_eqn(df)

p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="red", formula = y ~ x) +
geom_point() +
geom_text(x = 75, y = 90, label = labels[1], parse = TRUE,  check_overlap = TRUE ) +
geom_text(x = 75, y = 70, label = labels[2], parse = TRUE, check_overlap = TRUE )

print(p)
etienned
  • 21
  • 1
  • 4
  • How about this :https://stackoverflow.com/questions/7549694/adding-regression-line-equation-and-r2-on-graph – Jrakru56 Nov 15 '18 at 18:55
  • See these as well: https://stackoverflow.com/a/52704557/786542 & https://stackoverflow.com/a/49419755/786542 – Tung Nov 15 '18 at 20:14
  • Thank you #Jrakru56 for the link! – etienned Nov 16 '18 at 18:03
  • Thank you #Tung for the links! The first one actually helped a lot to fix some other issues I was having with displaying the linear regression line equation. – etienned Nov 16 '18 at 18:03

1 Answers1

2

In you function lm_eqn the format function gives your named vectors.By removing the names you solve your problem as follows:

set.seed(3L)
library(ggplot2)

df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
lm_eqn <- function(df){

  # browser()
  m <- lm(y ~ x, df)
  a <- coef(m)[1]
  a <- ifelse(sign(a) >= 0, 
              paste0(" + ", format(a, digits = 4)), 
              paste0(" - ", format(-a, digits = 4))  )

  b <-  format(coef(m)[2], digits=4)
  names(a) <- names(b) <-NULL
  eq1 <- substitute( paste( italic(y) == b, italic(x), a ),
                     list(a = a, 
                          b = b))

  eq2 <- substitute( paste( italic(R)^2 == r2 ), 
                     list(r2 = format(summary(m)$r.squared, digits = 3)))

  c( as.character(as.expression(eq1)), as.character(as.expression(eq2)))
}

labels <- lm_eqn(df)

p <- ggplot(data = df, aes(x = x, y = y)) +
  geom_smooth(method = "lm", se=FALSE, color="red", formula = y ~ x) +
  geom_point() +
  geom_text(x = 75, y = 90, label = labels[1], parse = TRUE,  check_overlap = TRUE ) +
  geom_text(x = 75, y = 70, label = labels[2], parse = TRUE, check_overlap = TRUE )

print(p)

Result:

enter image description here

GordonShumway
  • 1,980
  • 13
  • 19