【问题标题】:ggplot in R: add regression equation in a plotR中的ggplot:在图中添加回归方程
【发布时间】:2014-11-24 03:05:17
【问题描述】:

不久前,我从 Jayden 那里看到了关于将回归方程添加到绘图中的答案,我发现这非常有用。但是我不想显示R^2,所以我把代码改了一下:

lm_eqn = function(m) {
l <- list(a = format(coef(m)[1], digits = 2),
  b = format(abs(coef(m)[2]), digits = 2));
if (coef(m)[2] >= 0)  {
eq <- substitute(italic(y) == a + b %.% italic(x))
} else {
eq <- substitute(italic(y) == a - b %.% italic(x))    
}
as.character(as.expression(eq));                 
}

这设法将“a+bx”或“a-bx”绘制到图上,但没有实际系数替换 a 和 b。有谁知道如何解决这个问题?非常感谢!

杰登的回答:

 lm_eqn = function(m) {
 l <- list(a = format(coef(m)[1], digits = 2),
  b = format(abs(coef(m)[2]), digits = 2),
  r2 = format(summary(m)$r.squared, digits = 3));
 if (coef(m)[2] >= 0)  {
 eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
 } else {
 eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l)    
 }
 as.character(as.expression(eq));                 
 }

【问题讨论】:

    标签: r ggplot2 regression equation


    【解决方案1】:

    您似乎缺少substitute() 中的l。也就是说,使用substitute(yourFormula, l)。这是一个没有 r^2 的 MWE,与您正在查看的那个相似(我认为它位于 Adding Regression Line Equation and R2 on graph)。

    library(ggplot2)
    
    # Function to generate correlated data.
    GenCorrData = function(mu, Sig, n = 1000) {
      U            <- chol(Sig)
      Z            <- matrix(rnorm(n*length(mu)), nrow = length(mu))
      Y            <- crossprod(U,Z) + mu
      Y            <- as.data.frame(t(Y))
      names(Y)     <- c("x", "y")
      return(Y)
    }
    
    # Function to add text
    LinEqn = function(m) {
      l <- list(a = format(coef(m)[1], digits = 2),
                b = format(abs(coef(m)[2]), digits = 2));
      if (coef(m)[2] >= 0) {
        eq <- substitute(italic(y) == a + b %.% italic(x),l)
      } else {
        eq <- substitute(italic(y) == a - b %.% italic(x),l)    
      }
      as.character(as.expression(eq));                 
    }
    
    # Example
    set.seed(700)
    n1             <- 1000
    mu1            <- c(4, 5)
    Sig1           <- matrix(c(1, .8, .8, 1), nrow = length(mu1))
    df1            <- GenCorrData(mu1, Sig1, n1)
    scatter1       <- ggplot(data = df1, aes(x, y)) +
                        geom_point(shape = 21, color = "blue", size = 3.5) +
                        scale_x_continuous(expand = c(0, 0), limits = c(0, 8)) +
                        scale_y_continuous(expand = c(0, 0), limits = c(0, 8))
    scatter.line1  <- scatter1 + 
                        geom_smooth(method = "lm", formula = y ~ x, se = FALSE, 
                                    color="black", size = 1) +
                        annotate("text", x = 2, y = 7, color = "black", size = 5,
                                 label = LinEqn(lm(y ~ x, df1)), parse = TRUE)
    scatter.line1
    

    【讨论】:

    • 谢谢。我添加了 1,但随后收到如下错误消息:替换错误(斜体(y)== a - b %.% 斜体(x),1):指定的环境无效。我该如何解决这个问题?
    • @Bearbear, substitute() 正在寻找您的列表名称作为其第二个参数。你不是叫它l而不是1吗?
    • W,我把“l”写成数字“1”是个错误。现在可以了,非常感谢。
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 2011-11-24
    • 1970-01-01
    • 2018-08-28
    • 2013-03-16
    相关资源
    最近更新 更多