我们能否将回归方程与 R2 和 p 值巧妙地对齐? [英] Can we neatly align the regression equation and R2 and p value?

查看:17
本文介绍了我们能否将回归方程与 R2 和 p 值巧妙地对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将回归方程、R2 和 p 值(对于方程)巧妙地添加到 ggplot 图中的最佳(最简单)方法是什么?理想情况下,它应该与组和分面兼容.

What is the best (easiest) approach to add neatly to a ggplot plot the regression equation, the R2, and the p-value (for the equation)? Ideally it should be compatible with groups and faceting.

第一个图有回归方程加上 r2 和 p 值,使用 ggpubr 分组,但它们没有对齐?我错过了什么吗?它们可以作为一个字符串包含吗?

This first plot with has the regression equation plus the r2 and p-value by group using ggpubr, but they are not aligned? Am I missing something? Could they be included as one string?

library(ggplot)
library(ggpubr)

ggplot(mtcars, aes(x = wt, y = mpg, group = cyl))+
  geom_smooth(method="lm")+
  geom_point()+
  stat_regline_equation()+
  stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "*`,`~")),
           label.x.npc = "centre")

这是一个带有 ggpmisc 的选项,它的位置有些奇怪.
EDIT 奇怪的位置是由 geom=text 引起的,我已将其注释掉以提供更好的位置,并添加了 `label.x = "right" 以停止过度绘制.由于@dc37

Here is an option with ggpmisc, that does some odd placement.
EDIT Odd placement was caused by geom=text, which I've commented out to provide better placement, and added `label.x = "right" to stop overplotting. We still have misalignemnt as per ggpubr, due to the superscript issue flagged by @dc37

#https://stackoverflow.com/a/37708832/4927395
library(ggpmisc)

ggplot(mtcars, aes(x = wt, y = mpg, group = cyl))+
  geom_smooth(method="lm")+
  geom_point()+
  stat_poly_eq(formula = "y~x", 
             aes(label = paste(..eq.label.., ..rr.label.., sep = "*`,`~")), 
             parse = TRUE)+
  stat_fit_glance(method = 'lm',
                  method.args = list(formula = "y~x"),
                  #geom = 'text',

                  aes(label = paste("P-value = ", signif(..p.value.., digits = 4), sep = "")))

我确实找到了一个很好的解决方案,可以将相关的统计信息整合在一起,但这需要在 ggplot 之外创建回归,以及一堆字符串操作绒毛——这真的很简单吗?此外,它不(按当前编码)处理分组,也不处理分面.

I did find a good solution for bringing the relevant stats together, but that requires creating the regression outside ggplot, and a pile of string manipulation fluff - is this as easy as it gets? Also, it doesn't (as currently coded) deal to the grouping, and wouldn't deal with facetting.

#https://stackoverflow.com/a/51974753/4927395
#Solution as one string, equation, R2 and p-value
lm_eqn <- function(df, y, x){
  formula = as.formula(sprintf('%s ~ %s', y, x))
  m <- lm(formula, data=df);
  # formating the values into a summary string to print out
  # ~ give some space, but equal size and comma need to be quoted
  eq <- substitute(italic(target) == a + b %.% italic(input)*","~~italic(r)^2~"="~r2*","~~p~"="~italic(pvalue), 
                   list(target = y,
                        input = x,
                        a = format(as.vector(coef(m)[1]), digits = 2), 
                        b = format(as.vector(coef(m)[2]), digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3),
                        # getting the pvalue is painful
                        pvalue = format(summary(m)$coefficients[2,'Pr(>|t|)'], digits=1)
                   )
  )
  as.character(as.expression(eq));                 
}

ggplot(mtcars, aes(x = wt, y = mpg, group=cyl))+
  geom_point() +
  geom_text(x=3,y=30,label=lm_eqn(mtcars, 'wt','mpg'),color='red',parse=T) +
  geom_smooth(method='lm')

推荐答案

我已经更新了 'ggpmisc' 来简化此操作.0.3.4 版本正在向 CRAN 发送,源代码包已上线,二进制文件应在几天内构建.

I have updated 'ggpmisc' to make this easy. Version 0.3.4 is now on its way to CRAN, source package is on-line, binaries should be built in a few days' time.

library(ggpmisc) # version >= 0.3.4 !!

ggplot(mtcars, aes(x = wt, y = mpg, group = cyl)) +
  geom_smooth(method="lm")+
  geom_point()+
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..eq.label.., ..rr.label.., ..p.value.label.., sep = "*`,`~")), 
               parse = TRUE,
               label.x.npc = "right",
               vstep = 0.05) # sets vertical spacing

这篇关于我们能否将回归方程与 R2 和 p 值巧妙地对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆