1
library(ggplot2)
library(dplyr)
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"),
                      facet = c("C", "D", "E", "F", "G"),
                      y2 = y * c(0.5,2),
                      w = sqrt(x))

formula <- y ~ poly(x, 3, raw = TRUE)
ggplot(my.data %>% group_by(facet, group) %>% mutate(n = n()), aes(x, y, n = n, color = facet)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  facet_grid(vars(group)) + 
  ggpmisc::stat_poly_eq(aes(label = paste(stat(rr.label), paste("N ~`=`~", n), sep = "*\", \"*")), 
                        formula = formula, parse=T)

geom_smoothトレンドラインを描画するために数学を一度計算します。次に を追加するggpmisc::stat_poly_eqと、もう一度数学を再計算してラベルを取得しますか?

4

1 に答える 1

1

はい、2回フィットします。stat + geom の新しいペアを作成することに頼らずに、「ggplot2」内でこれを回避する方法がわかりません。それは可能だと思いますし、実際にはパッケージの優れた機能強化になるでしょう。

于 2021-06-03T11:54:03.530 に答える