简体   繁体   中英

How to fix the labeling of a regression line equation on graph (using ggplot2)

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)

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM