我想以与 geom_smooth(method="lm")
类似的方式包含来自 geom_quantile()
拟合线的相关统计数据> 拟合线性回归(我之前使用的是 ggpmisc,即 awesome)。例如,这段代码:
# quantile regression example with ggpmisc equation
# basic quantile code from here:
# https://ggplot2.tidyverse.org/reference/geom_quantile.html
library(tidyverse)
library(ggpmisc)
# see ggpmisc vignette for stat_poly_eq() code below:
# https://cran.r-project.org/web/packages/ggpmisc/vignettes/user-guide.html#stat_poly_eq
my_formula <- y ~ x
#my_formula <- y ~ poly(x, 3, raw = TRUE)
# linear ols regression with equation labelled
m <- ggplot(mpg, aes(displ, 1 / hwy)) +
geom_point()
m +
geom_smooth(method = "lm", formula = my_formula) +
stat_poly_eq(aes(label = paste(stat(eq.label), "*\" with \"*",
stat(rr.label), "*\", \"*",
stat(f.value.label), "*\", and \"*",
stat(p.value.label), "*\".\"",
sep = "")),
formula = my_formula, parse = TRUE, size = 3)
对于分位数回归,您可以将 geom_smooth()
换成 geom_quantile()
并绘制一条可爱的分位数回归线(在本例中为中位数):
# quantile regression - no equation labelling
m +
geom_quantile(quantiles = 0.5)
您如何将摘要统计数据输出到标签中,或者在移动中重新创建它们? (即除了在调用 ggplot 之前进行回归然后将其传递给然后注释(例如类似于为线性回归所做的 here 或 here?
最佳答案
请将此视为 Pedro 出色回答的附录,他在其中完成了大部分繁重的工作 - 这增加了一些演示调整(颜色和线型)和代码以简化多个分位数,生成如下图:
library(tidyverse)
library(ggpmisc) #ensure version 0.3.8 or greater
library(quantreg)
library(generics)
my_formula <- y ~ x
#my_formula <- y ~ poly(x, 3, raw = TRUE)
# base plot
m <- ggplot(mpg, aes(displ, 1 / hwy)) +
geom_point()
# function for labelling
# Doesn't neatly handle P values (e.g return "P<0.001 where appropriate)
stat_rq_eqn <- function(formula = y ~ x, tau = 0.5, colour = "red", label.y = 0.9, ...) {
stat_fit_tidy(method = "rq",
method.args = list(formula = formula, tau = tau),
tidy.args = list(se.type = "nid"),
mapping = aes(label = sprintf('italic(tau)~"="~%.3f~";"~y~"="~%.3g~+~%.3g~x*", with "~italic(P)~"="~%.3g',
after_stat(x_tau),
after_stat(Intercept_estimate),
after_stat(x_estimate),
after_stat(x_p.value))),
parse = TRUE,
colour = colour,
label.y = label.y,
...)
}
# This works, though with double entry of plot specs
# custom colours and linetype
# https://stackoverflow.com/a/44383810/4927395
# https://stackoverflow.com/a/64518380/4927395
m +
geom_quantile(quantiles = c(0.1, 0.5, 0.9),
aes(colour = as.factor(..quantile..),
linetype = as.factor(..quantile..))
)+
scale_color_manual(values = c("red","purple","darkgreen"))+
scale_linetype_manual(values = c("dotted", "dashed", "solid"))+
stat_rq_eqn(tau = 0.1, colour = "red", label.y = 0.9)+
stat_rq_eqn(tau = 0.5, colour = "purple", label.y = 0.95)+
stat_rq_eqn(tau = 0.9, colour = "darkgreen", label.y = 1.0)+
theme(legend.position = "none") # suppress legend
# not a good habit to have double entry above
# modified with reference to tibble for plot specs,
# though still a stat_rq_eqn call for each quantile and manual vertical placement
# https://www.r-bloggers.com/2019/06/curly-curly-the-successor-of-bang-bang/
my_tau = c(0.1, 0.5, 0.9)
my_colours = c("red","purple","darkgreen")
my_linetype = c("dotted", "dashed", "solid")
quantile_plot_specs <- tibble(my_tau, my_colours, my_linetype)
m +
geom_quantile(quantiles = {{quantile_plot_specs$my_tau}},
aes(colour = as.factor(..quantile..),
linetype = as.factor(..quantile..))
)+
scale_color_manual(values = {{quantile_plot_specs$my_colours}})+
scale_linetype_manual(values = {{quantile_plot_specs$my_linetype}})+
stat_rq_eqn(tau = {{quantile_plot_specs$my_tau[1]}}, colour = {{quantile_plot_specs$my_colours[1]}}, label.y = 0.9)+
stat_rq_eqn(tau = {{quantile_plot_specs$my_tau[2]}}, colour = {{quantile_plot_specs$my_colours[2]}}, label.y = 0.95)+
stat_rq_eqn(tau = {{quantile_plot_specs$my_tau[3]}}, colour = {{quantile_plot_specs$my_colours[3]}}, label.y = 1.0)+
theme(legend.position = "none")
关于r - 是否有一种巧妙的方法可以使用来自 geom_quantile() 的方程和其他统计数据来标记 ggplot 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65695409/