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

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

问题描述

将整洁地添加到 ggplot 图中的回归方程式,R2和p值(对于方程式)是什么(最好)?理想情况下,它应该与组和构面兼容.

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.

使用 ggpubr 按组的第一个图具有回归方程式以及r2和p值,但是它们没有对齐吗?我想念什么吗?可以将它们作为一个字符串包含在内吗?

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"来防止过度绘图.由于 ggpubr ,由于@ 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天全站免登陆