在SEPARATE LINES图

时间:2018-03-22 00:15:30

标签: r ggplot2 regression ggpmisc

几年前,一张海报询问如何在下面的链接中的ggplot图上添加回归线方程和R2。

Adding Regression Line Equation and R2 on graph

最重要的解决方案是:

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

p1 <- p + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)

我正在使用此代码,效果很好。但是,我想知道是否有可能使这段代码在单独的行上具有R2值和回归线方程,而不是用逗号分隔。

而不是像这样

Instead of like this

像这样的东西

Something like this

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

编辑:

除了插入等式,我还修复了截距值的符号。通过将RNG设置为set.seed(2L),将给出正截距。以下示例产生负截距。

我还修复了geom_text

中的重叠文字
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)

enter image description here

答案 1 :(得分:3)

ggpmisc包具有stat_poly_eq功能,专门为此任务构建(不仅适用于线性回归)。使用与@Sathish发布的data相同的label.y.npc,我们可以单独添加等式和R2,但给出label.x.npc个不同的值。如果需要,library(ggplot2) library(ggpmisc) set.seed(21318) df <- data.frame(x = c(1:100)) df$y <- 2 + 3*df$x + rnorm(100, sd = 40) formula1 <- y ~ x ggplot(data = df, aes(x = x, y = y)) + geom_point() + geom_smooth(method = "lm", se = FALSE, formula = formula1) + stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~")), label.x.npc = "right", label.y.npc = 0.15, eq.with.lhs = "italic(hat(y))~`=`~", eq.x.rhs = "~italic(x)", formula = formula1, parse = TRUE, size = 5) + stat_poly_eq(aes(label = paste(..rr.label.., sep = "~~~")), label.x.npc = "right", label.y.npc = "bottom", formula = formula1, parse = TRUE, size = 5) + theme_bw(base_size = 16) 可以调整。

    User user;
    userDAO = AppDatabase.getAppDatabase(getApplicationContext()).userDao();
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            user = userDAO.getUser("testuser1");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String name = user.getName();
                }
            });
        }
    });
    thread.start();

reprex package(v0.2.0)创建于2018-03-21。