如何修复图形上回归线方程的标签(使用ggplot2)

时间:2018-11-15 18:44:05

标签: r ggplot2 graph rstudio

当我在rstudio中运行以下代码(见下文)时,除线性回归线方程式之外,其他所有内容看起来都很不错。 而不是y = 3.142x -4.751,而是y = c(3.142)xc(-4.751)。

该如何解决? 预先非常感谢。

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)

1 个答案:

答案 0 :(得分:2)

在函数lm_eqn中,format函数给出了命名向量。通过删除名称,可以如下解决问题:

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)

结果:

enter image description here