ggpmisc :: stat_poly_eq

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,formatC(stat(r.squared))))) 

ggpmisc :: stat_poly_eq


编辑

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

ggpmisc :: stat_poly_eq

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

xsa591017 回答:ggpmisc :: stat_poly_eq

好,我知道了。

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,formatC(stat(coef.ls)[[2]][[2,formatC(stat(r.squared))))) 

enter image description here

,

'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,formula = formula) +
  stat_poly_eq(formula = formula,aes(label = ""),geom = "debug") 

哪个打印到控制台,两个小标题,每个面板一个: enter image description here

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

ggplot(my.data,summary.fun = function(x) {x[["coef.ls"]][[1]]})

仅打印coefs.ls

enter image description here

我最近根据一个建议添加了"numeric"选项,在这个示例中,我注意到了一个错误:aes(label = "")应该不是必需的,但因为{{1 }}审美是错误的。我将在下一个版本中解决此问题。

本文链接:https://www.f2er.com/3109004.html

大家都在问