如何使用 ggpmisc::stat_poly_eq 将观察数添加到绘图标签(使用分组和分面)
How to add number of observations to label of plot (with grouping and faceting) using ggpmisc::stat_poly_eq
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)
ggplot2::ggplot(my.data, ggplot2::aes(x, y, color = group)) +
ggplot2::geom_point() +
ggplot2::geom_smooth(method = "lm", formula = formula) +
ggplot2::facet_grid(facet ~ .) +
ggpmisc::stat_poly_eq(
ggplot2::aes(label = paste(
stat(rr.label),
sep = "*\", \"*")),
formula = formula, parse=T)
实际输出:
预期输出:
其中每个颜色组和每个事实图都会出现 N = X
,其中 X 是该组和方面中的点数。
在版本 0.4.0.9000 中更新了软件包 'ggpmisc',因此 stat_poly_eq()
现在也 returns n
和 n.label
。截至 2021 年 9 月 3 日,'ggpmisc' 的 CRAN 版本为 0.4.3。为了完整起见,我在这里添加了完整的代表。
library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
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, aes(x, y, color = group)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(aes(label = paste(after_stat(rr.label),
after_stat(n.label),
sep = "*\", \"*")),
formula = formula,
vstep = 0.2) +
facet_grid(facet ~ .)
由 reprex package (v2.0.1)
于 2021-09-04 创建
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)
ggplot2::ggplot(my.data, ggplot2::aes(x, y, color = group)) +
ggplot2::geom_point() +
ggplot2::geom_smooth(method = "lm", formula = formula) +
ggplot2::facet_grid(facet ~ .) +
ggpmisc::stat_poly_eq(
ggplot2::aes(label = paste(
stat(rr.label),
sep = "*\", \"*")),
formula = formula, parse=T)
实际输出:
预期输出:
其中每个颜色组和每个事实图都会出现 N = X
,其中 X 是该组和方面中的点数。
在版本 0.4.0.9000 中更新了软件包 'ggpmisc',因此 stat_poly_eq()
现在也 returns n
和 n.label
。截至 2021 年 9 月 3 日,'ggpmisc' 的 CRAN 版本为 0.4.3。为了完整起见,我在这里添加了完整的代表。
library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
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, aes(x, y, color = group)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(aes(label = paste(after_stat(rr.label),
after_stat(n.label),
sep = "*\", \"*")),
formula = formula,
vstep = 0.2) +
facet_grid(facet ~ .)
由 reprex package (v2.0.1)
于 2021-09-04 创建