1

ggpmisc::stat_poly_eq有一个选项output.type = "numeric"允许获得拟合模型参数的估计值。下面是我尝试将它与facet_wrap. 每个方面我得到不同的结果,但两个方面的系数相同。我做错了什么,还是一个错误?

library(ggpmisc)

set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, 
                      y = y,
                      group = c("A", "B"))
my.data[my.data$group=="A",]$y <- my.data[my.data$group=="A",]$y + 200000

formula <- y ~ poly(x, 1, raw = TRUE)

myformat <- "Intercept: %s\nSlope: %s\nR²: %s"
ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, output.type = "numeric",
               mapping = aes(label = 
                               sprintf(myformat,
                                       formatC(stat(coef.ls)[[1]][[1, "Estimate"]]),
                                       formatC(stat(coef.ls)[[1]][[2, "Estimate"]]),
                                       formatC(stat(r.squared))))) 

在此处输入图像描述


编辑

我们必须抓住面板编号。奇怪的是formatC(stat(as.integer(PANEL)))返回每个方面的面板编号:

在此处输入图像描述

但是formatC(stat(coef.ls)[[stat(as.integer(PANEL))]][[1, "Estimate"]])不起作用,因为这里PANEL = c(1,2).

4

2 回答 2

1

好的,我想通了。

ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(
    formula = formula, output.type = "numeric",
    mapping = aes(label = 
                    sprintf(myformat,
                            c(formatC(stat(coef.ls)[[1]][[1, "Estimate"]]), 
                              formatC(stat(coef.ls)[[2]][[1, "Estimate"]])),
                            c(formatC(stat(coef.ls)[[1]][[2, "Estimate"]]), 
                              formatC(stat(coef.ls)[[2]][[2, "Estimate"]])),
                            formatC(stat(r.squared))))) 

在此处输入图像描述

于 2019-11-13T16:45:19.373 回答
0

'ggpmisc' 的 0.3.2 版现在在 CRAN 中。本周早些时候提交。在文档中,我现在给出了一些使用geom_debug()我的包 'gginnards' 的示例来查看 stats 返回的数据框(可与任何 ggplot stat 一起使用或单独使用)。对于您的示例,它将像这样工作:

library(ggpmisc)
library(gginnards)

set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, 
                      y = y,
                      group = c("A", "B"))
my.data[my.data$group=="A",]$y <- my.data[my.data$group=="A",]$y + 200000

formula <- y ~ poly(x, 1, raw = TRUE)

myformat <- "Intercept: %s\nSlope: %s\nR²: %s"
ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, output.type = "numeric",
               aes(label = ""),
               geom = "debug") 

打印到控制台,两个小标题,每个面板一个: 在此处输入图像描述

以下示例添加到地址注释:

ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, output.type = "numeric",
               aes(label = ""),
               summary.fun = function(x) {x[["coef.ls"]][[1]]})

只打印coefs.ls.

在此处输入图像描述

"numeric"最近添加了该选项以响应建议,并且在此示例中我注意到了一个错误:aes(label = "")不应该需要,但需要,因为label美学的默认映射是错误的。我会在下一个版本中解决这个问题。

于 2019-11-14T18:20:57.057 回答