【问题标题】:Coefficients per facet with output.type="numeric" in ggpmisc::stat_poly_eqggpmisc::stat_poly_eq 中 output.type="numeric" 的每个方面的系数
【发布时间】:2019-11-13 15:03:41
【问题描述】:

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

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)。

【问题讨论】:

    标签: r ggplot2 ggpmisc


    【解决方案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))))) 
    

    【讨论】:

      【解决方案2】:

      'ggpmisc' 的 0.3.2 版现已在 CRAN 中。本周早些时候提交。在文档中,我现在给出了一些使用我的包 'gginnards' 中的geom_debug() 的示例,以查看 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 美学的默认映射是错误的所以需要。我会在下一个版本中解决这个问题。

      【讨论】:

      • 是的,我试过geom = "debug"。但是我们在打印输出中看不到coef.ls 列表的内容。如果我们可以存储此打印输出,那就太好了。
      • 是的,有一点点诡计是可能的。请参阅上面对我之前的答案的补充。感谢您报告此事。我确实需要扩展“gginnards”的文档。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2022-01-15
      • 2016-12-05
      • 1970-01-01
      • 2015-07-29
      • 2021-07-23
      相关资源
      最近更新 更多