ggplot2 添加拟合线及公式
主要介绍ggplot2里拟合线与公式设置
使用ggpmisc()
stat_smooth + stat_poly_eq组合实现拟合曲线以及拟合公式显示。
stat_poly_eq用于显示公式,支持以下统计参数显示[1]:
eq.label
equation for the fitted polynomial as a character string to be parsed
rr.label
R2 of the fitted model as a character string to be parsed
adj.rr.label
Adjusted R2 of the fitted model as a character string to be parsed
f.value.label
F value and degrees of freedom for the fitted model as a whole.
p.value..label
P-value for the F-value above.
AIC.label
AIC for the fitted model.
BIC.label
BIC for the fitted model.
使用
# 定义公式类型
formula <- y ~ x + I(x^2)
# 添加拟合线
P + geom_smooth(method = "lm", formula = formula)
# 添加公式
p + stat_poly_eq(aes(label = paste(stat(eq.label), stat(adj.rr.label),
sep = "*\", \"*")), formula = formula, parse = TRUE)
# 添加参数表
p + stat_fit_tb(method = "lm",
method.args = list(formula = formula),
tb.type = "fit.anova",
tb.vars = c(Effect = "term",
"df",
"M.S." = "meansq",
"italic(F)" = "statistic",
"italic(P)" = "p.value"),
label.y = 0.87, label.x = 0.1,
size = 4,
parse = TRUE
)
stat_poly_eq显示p-valuestat(p.value.label)
,p显示为大写[2],如果需要改为小写斜体[3]
# 将stat(p.value.label) 替换为
paste("italic(p)", substring(p.value.label,10))
对数指数形式
stat_poly_eq
在显示指数或对数函数时总出现问题,因此考虑采用geom_line + geom_ribbon + annotate方式迂回实现[4]。
# 公式拟合
log.model <-lm(log(Frequency) ~ Coverage, area)
# 用于geom_ribbon显示CI
observeddata <- cbind(area, predict(log.model, newdata = area, interval = 'confidence'))
# 组合生成拟合线与公式
p + geom_ribbon(data = observeddata,
aes(x = Coverage, y = exp(fit), ymin = exp(lwr), ymax = exp(upr)), ...) +
geom_line(data = log.model.df, aes(Coverage, y, color = 'b'), ...) +
annotate("text", ...)
使用ggPredict()
ggPredict
提供了拟合曲线的封装函数,相比ggplot2原生绘制,更为简单,同时支持lm(),glm()等拟合方式[5]。
安装
install.packages("devtools")
devtools::install_github("cardiomoon/ggiraphExtra")
对比原生方法
# 载入样例数据
require(moonBook)
# 拟合
fit = lm(NTAV~age, data = radial)
# 使用ggplot的 geom_smooth 绘制
ggplot(radial, aes(y = NTAV,x = age)) + geom_point() + geom_smooth(method = "lm")
# 使用ggPredict绘制
ggPredict(fit, se = TRUE, interactive = TRUE)