1

ggpmisc::stat_poly_eqoutput.type = "numeric"適合モデルのパラメーターの推定値を取得できるオプションがあります。以下は、それを使用する私の試みfacet_wrapです。ファセットごとに異なる結果が得られますが、係数は 2 つのファセットで同じです。私は何か間違ったことをしていますか、それともバグですか?

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 に答える