这现在在 GitHub 的包 'ggpmisc' 中实现(未来版本 0.4.4),但尚未在 CRAN 中可用的版本 0.4.3 中实现。这并不完全按照您的要求做,因为目前geom_poly_eq()
不会返回R,因为它对于高于 1 的多项式没有意义。保持图形范式的语法允许更容易定制,但代价是比全输入更长的代码-一个来自包的函数pubr
。每种方法都有其优点和缺点。我在这里展示了三个使用包'ggpmisc'(连同'ggplot2')的不同可能方法的例子。
library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
# First approach: faint lines for non-significant fits, with bands
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
stat_poly_eq(aes(label = paste(after_stat(rr.label),
after_stat(p.value.label),
sep = "*\", \"*")),
label.x = "right") +
stat_poly_line(aes(colour = stage(after_scale = ifelse(p.value < 0.05,
alpha(colour, 1),
alpha(colour, 0.25)))),
se = TRUE,
mf.values = T) +
facet_wrap(~class, ncol = 2) +
theme_bw()

# Second approach: faint lines for non-significant fits, no-bands
# colour mapped to class
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
stat_poly_eq(aes(label = paste(after_stat(rr.label),
after_stat(p.value.label),
sep = "*\", \"*")),
label.x = "right") +
stat_poly_line(aes(colour = stage(start = class,
after_scale = ifelse(p.value < 0.05,
alpha(colour, 1),
alpha(colour, 0.25)))),
se = FALSE,
mf.values = T) +
facet_wrap(~class, ncol = 2) +
theme_bw()
#> Warning: Failed to apply `after_scale()` modifications to legend

# Third approach: no bands or lines for non-significant fits
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
stat_poly_eq(aes(label = paste(after_stat(rr.label),
after_stat(p.value.label),
sep = "*\", \"*")),
label.x = "right") +
stat_poly_line(aes(colour = stage(after_scale = ifelse(p.value < 0.05,
colour,
NA)),
fill = stage(after_scale = ifelse(p.value < 0.05,
fill,
NA))),
se = TRUE,
mf.values = T) +
facet_wrap(~class, ncol = 2) +
theme_bw()
#> Warning: Duplicated aesthetics after name standardisation: NA
#> Warning: Failed to apply `after_scale()` modifications to legend

由reprex 包(v2.0.1)于 2021-09-07 创建