我想知道如何在 ggplot
上添加回归线方程和 R^2。我的代码是:
library(ggplot2)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
p
任何帮助将不胜感激。
latticeExtra::lmlineq()
。
Error: 'lmlineq' is not an exported object from 'namespace:latticeExtra'
这是一种解决方案
# GET EQUATION AND R-SQUARED AS STRING
# SOURCE: https://groups.google.com/forum/#!topic/ggplot2/1TgH-kG5XMA
lm_eqn <- function(df){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(unname(coef(m)[1]), digits = 2),
b = format(unname(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}
p1 <- p + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)
编辑。我找出了我选择此代码的来源。这是 ggplot2 google 组中原始帖子的 link
https://i.stack.imgur.com/7aq55.png
我的包 ggpmisc
中的统计信息 stat_poly_eq()
使添加基于线性模型拟合的文本标签成为可能。
此答案已于 2022 年 6 月 2 日针对 'ggpmisc' (>= 0.4.0) 和 'ggplot2' (>= 3.3.0) 更新。在示例中,我使用 stat_poly_line()
而不是 stat_smooth()
,因为它与 method
和 formula
的 stat_poly_eq()
具有相同的默认值。我在所有代码示例中都省略了 stat_poly_line()
的附加参数,因为它们与添加标签的问题无关。
library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
# artificial data
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
df$yy <- 2 + 3 * df$x + 0.1 * df$x^2 + rnorm(100, sd = 40)
# using default formula, label and methods
ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq() +
geom_point()
https://i.imgur.com/RDZ2XTj.png
# assembling a single label with equation and R2
ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point()
https://i.imgur.com/1moN0zF.png
# adding separate labels with equation and R2
ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq(aes(label = after_stat(eq.label))) +
stat_poly_eq(label.y = 0.9) +
geom_point()
https://i.imgur.com/Gv3qsgl.png
# regression through the origin
ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line(formula = y ~ x + 0) +
stat_poly_eq(formula = y ~ x + 0, aes(label = after_stat(eq.label))) +
geom_point()
https://i.imgur.com/xux4jvx.png
# fitting a polynomial
ggplot(data = df, aes(x = x, y = yy)) +
stat_poly_line(formula = y ~ poly(x, 2, raw = TRUE)) +
stat_poly_eq(formula = y ~ poly(x, 2, raw = TRUE),
aes(label = after_stat(eq.label))) +
geom_point()
https://i.imgur.com/ss2mXll.png
# adding a hat as asked by @MYaseen208 and @elarry
ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq(eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point()
https://i.imgur.com/Eb2kLV9.png
# variable substitution as asked by @shabbychef
# same labels in equation and axes
ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq(eq.with.lhs = "italic(h)~`=`~",
eq.x.rhs = "~italic(z)",
aes(label = after_stat(eq.label))) +
labs(x = expression(italic(z)), y = expression(italic(h))) +
geom_point()
https://i.imgur.com/S6xxz2v.png
# grouping as asked by @helen.h
dfg <- data.frame(x = c(1:100))
dfg$y <- 20 * c(0, 1) + 3 * df$x + rnorm(100, sd = 40)
dfg$group <- factor(rep(c("A", "B"), 50))
ggplot(data = dfg, aes(x = x, y = y, colour = group)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point()
https://i.imgur.com/lTEynGy.png
ggplot(data = dfg, aes(x = x, y = y, linetype = group, grp.label = group)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(grp.label), "*\": \"*",
after_stat(eq.label), "*\", \"*",
after_stat(rr.label), sep = ""))) +
geom_point()
https://i.imgur.com/TfBBBCu.png
# a single fit with grouped data as asked by @Herman
ggplot(data = dfg, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point(aes(colour = group))
https://i.imgur.com/BlWBPRk.png
# facets
ggplot(data = dfg, aes(x = x, y = y)) +
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +
geom_point() +
facet_wrap(~group)
https://i.imgur.com/6BIxWGj.png
由 reprex package (v2.0.1) 于 2022 年 6 月 2 日创建
x
和y
是指绘图层中的x
和y
数据,不一定是my.formula
构建时范围内的数据。因此,公式应该总是使用 x 和 y 变量吗?
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~"))
可以完成这项工作。
stat_poly_eq()
返回的数据中没有预定义的 r.label 。您可以使用 stat_fit_glance()
,同样来自包 'ggpmisc',它将 R2 作为数值返回。请参阅帮助页面中的示例,并将 stat(r.squared)
替换为 sqrt(stat(r.squared))
。
我更改了 stat_smooth
和相关函数的源代码的几行,以创建一个添加拟合方程和 R 平方值的新函数。这也适用于构面图!
library(devtools)
source_gist("524eade46135f6348140")
df = data.frame(x = c(1:100))
df$y = 2 + 5 * df$x + rnorm(100, sd = 40)
df$class = rep(1:2,50)
ggplot(data = df, aes(x = x, y = y, label=y)) +
stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE) +
geom_smooth(method="lm",se=FALSE) +
geom_point() + facet_wrap(~class)
https://i.stack.imgur.com/onecx.png
我使用@Ramnath 的答案中的代码来格式化等式。 stat_smooth_func
函数不是很健壮,但使用它应该不难。
https://gist.github.com/kdauria/524eade46135f6348140。如果出现错误,请尝试更新 ggplot2
。
stat_smooth_func(mapping=aes(group=cut(x.val,c(-70,-20,0,20,50,130))),geom="text",method="lm",hjust=0,parse=TRUE)
,结合来自 stackoverflow.com/questions/19735149/… 的 EvaluateSmooths
source
脚本中的整个文件。
xpos
和 ypos
参数。因此,如果您希望所有方程重叠,只需设置 xpos
和 ypos
。否则,根据数据计算 xpos
和 ypos
。如果你想要更高级的东西,在函数中添加一些逻辑应该不会太难。例如,也许您可以编写一个函数来确定图形的哪一部分空间最多,然后将函数放在那里。
我已将 Ramnath 的帖子修改为 a) 使其更通用,因此它接受线性模型作为参数而不是数据框,并且 b) 更恰当地显示底片。
lm_eqn = function(m) {
l <- list(a = format(coef(m)[1], digits = 2),
b = format(abs(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3));
if (coef(m)[2] >= 0) {
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
} else {
eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
}
as.character(as.expression(eq));
}
用法将更改为:
p1 = p + geom_text(aes(x = 25, y = 300, label = lm_eqn(lm(y ~ x, df))), parse = TRUE)
p1 = p + annotate("text", x = 25, y = 300, label = lm_eqn(lm(y ~ x, df)), colour="black", size = 5, parse=TRUE)
编辑:这也解决了您在图例中显示字母时可能遇到的任何问题。
"cannot coerce class "lm" to a data.frame"
。此替代方法有效:df.labs <- data.frame(x = 25, y = 300, label = lm_eqn(df))
和 p <- p + geom_text(data = df.labs, aes(x = x, y = y, label = label), parse = TRUE)
lm_eqn(lm(...))
,您会收到错误消息。您可能在尝试了那个之后又尝试了这个,但忘记确保您已经重新定义了 lm_eqn
这里给大家最简单的代码
注意:显示 Pearson 的 Rho 而不是 R^2。
library(ggplot2)
library(ggpubr)
df <- data.frame(x = c(1:100)
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()+
stat_cor(label.y = 35)+ #this means at 35th unit in the y axis, the r squared and p value will be shown
stat_regline_equation(label.y = 30) #this means at 30th unit regresion line equation will be shown
p
https://i.stack.imgur.com/f4FGl.png
stat_cor(aes(label = ..rr.label..))
stat_regline_equation()
结合起来绘制回归方程
stat_regline_equation()
和 stat_cor()
中的大部分代码都是未经我的包 'ggpmisc' 确认而复制的。它取自 stat_poly_eq()
,它得到了积极的维护,并在复制后获得了一些新功能。示例代码需要最少的编辑才能使用“ggpmisc”。
stat_poly_eq()
更麻烦。我不能轻易地将 label=..eq.label..
和 label=..rr.label..
分成单独的行并直观地将它们放在网格上,这意味着我将继续更喜欢 stat_cor()
和 stat_regline_equation()
。这当然是我个人的观点,可能不会被其他用户分享,但只是一些需要考虑的事情,因为您仍在积极更新ggpmisc
使用 ggpubr:
library(ggpubr)
# reproducible data
set.seed(1)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
# By default showing Pearson R
ggscatter(df, x = "x", y = "y", add = "reg.line") +
stat_cor(label.y = 300) +
stat_regline_equation(label.y = 280)
https://i.stack.imgur.com/Xb8KN.png
# Use R2 instead of R
ggscatter(df, x = "x", y = "y", add = "reg.line") +
stat_cor(label.y = 300,
aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~"))) +
stat_regline_equation(label.y = 280)
## compare R2 with accepted answer
# m <- lm(y ~ x, df)
# round(summary(m)$r.squared, 2)
# [1] 0.85
https://i.stack.imgur.com/lbtt5.png
label.y
指定一个数字?
label.y = max(df$y) * 0.8
真的很喜欢@Ramnath 解决方案。为了允许使用自定义回归公式(而不是固定为 y 和 x 作为文字变量名称),并将 p 值添加到打印输出中(正如@Jerry T 评论的那样),这里是 mod:
lm_eqn <- function(df, y, x){
formula = as.formula(sprintf('%s ~ %s', y, x))
m <- lm(formula, data=df);
# formating the values into a summary string to print out
# ~ give some space, but equal size and comma need to be quoted
eq <- substitute(italic(target) == a + b %.% italic(input)*","~~italic(r)^2~"="~r2*","~~p~"="~italic(pvalue),
list(target = y,
input = x,
a = format(as.vector(coef(m)[1]), digits = 2),
b = format(as.vector(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3),
# getting the pvalue is painful
pvalue = format(summary(m)$coefficients[2,'Pr(>|t|)'], digits=1)
)
)
as.character(as.expression(eq));
}
geom_point() +
ggrepel::geom_text_repel(label=rownames(mtcars)) +
geom_text(x=3,y=300,label=lm_eqn(mtcars, 'hp','wt'),color='red',parse=T) +
geom_smooth(method='lm')
https://i.stack.imgur.com/m3G4L.png
ggplot(mtcars, aes(x = wt, y = mpg, group=cyl))+
?一个半相关的问题 - 如果我们在 aes()
中为 ggplot 引用 hp 和 wt,那么我们可以 grab 将它们用于对 lm_eqn
的调用,那么我们只需要在一个地方编码?我知道我们可以在调用 ggplot() 之前设置 xvar = "hp"
,并在两个位置都使用 xvar 来替换 hp,但是这感觉应该是不必要的。
另一种选择是创建使用 dplyr
和 broom
库生成方程的自定义函数:
get_formula <- function(model) {
broom::tidy(model)[, 1:2] %>%
mutate(sign = ifelse(sign(estimate) == 1, ' + ', ' - ')) %>% #coeff signs
mutate_if(is.numeric, ~ abs(round(., 2))) %>% #for improving formatting
mutate(a = ifelse(term == '(Intercept)', paste0('y ~ ', estimate), paste0(sign, estimate, ' * ', term))) %>%
summarise(formula = paste(a, collapse = '')) %>%
as.character
}
lm(y ~ x, data = df) -> model
get_formula(model)
#"y ~ 6.22 + 3.16 * x"
scales::percent(summary(model)$r.squared, accuracy = 0.01) -> r_squared
现在我们需要将文本添加到绘图中:
p +
geom_text(x = 20, y = 300,
label = get_formula(model),
color = 'red') +
geom_text(x = 20, y = 285,
label = r_squared,
color = 'blue')
https://i.stack.imgur.com/YwvAi.png
受 this answer 中提供的方程样式的启发,一种更通用的方法(多个预测器 + 乳胶输出作为选项)可以是:
print_equation= function(model, latex= FALSE, ...){
dots <- list(...)
cc= model$coefficients
var_sign= as.character(sign(cc[-1]))%>%gsub("1","",.)%>%gsub("-"," - ",.)
var_sign[var_sign==""]= ' + '
f_args_abs= f_args= dots
f_args$x= cc
f_args_abs$x= abs(cc)
cc_= do.call(format, args= f_args)
cc_abs= do.call(format, args= f_args_abs)
pred_vars=
cc_abs%>%
paste(., x_vars, sep= star)%>%
paste(var_sign,.)%>%paste(., collapse= "")
if(latex){
star= " \\cdot "
y_var= strsplit(as.character(model$call$formula), "~")[[2]]%>%
paste0("\\hat{",.,"_{i}}")
x_vars= names(cc_)[-1]%>%paste0(.,"_{i}")
}else{
star= " * "
y_var= strsplit(as.character(model$call$formula), "~")[[2]]
x_vars= names(cc_)[-1]
}
equ= paste(y_var,"=",cc_[1],pred_vars)
if(latex){
equ= paste0(equ," + \\hat{\\varepsilon_{i}} \\quad where \\quad \\varepsilon \\sim \\mathcal{N}(0,",
summary(MetamodelKdifEryth)$sigma,")")%>%paste0("$",.,"$")
}
cat(equ)
}
model
参数需要一个 lm
对象,latex
参数是一个布尔值,用于请求简单字符或乳胶格式的方程,...
参数将其值传递给 format
函数。
我还添加了一个将其输出为乳胶的选项,因此您可以在 rmarkdown 中使用此函数,如下所示:
```{r echo=FALSE, results='asis'}
print_equation(model = lm_mod, latex = TRUE)
```
现在使用它:
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
df$z <- 8 + 3 * df$x + rnorm(100, sd = 40)
lm_mod= lm(y~x+z, data = df)
print_equation(model = lm_mod, latex = FALSE)
此代码产生:y = 11.3382963933174 + 2.5893419 * x + 0.1002227 * z
如果我们要求一个乳胶方程,将参数四舍五入为 3 位数:
print_equation(model = lm_mod, latex = TRUE, digits= 3)
https://i.stack.imgur.com/D38Kz.png
类似于 @zx8754 和 @kdauria 的答案,除了使用 ggplot2
和 ggpubr
。我更喜欢使用 ggpubr
,因为它不需要自定义函数,例如此问题的最佳答案。
library(ggplot2)
library(ggpubr)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
ggplot(data = df, aes(x = x, y = y)) +
stat_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() +
stat_cor(aes(label = paste(..rr.label..)), # adds R^2 value
r.accuracy = 0.01,
label.x = 0, label.y = 375, size = 4) +
stat_regline_equation(aes(label = ..eq.label..), # adds equation to linear regression
label.x = 0, label.y = 400, size = 4)
https://i.stack.imgur.com/uT8oz.jpg
也可以在上图中添加 p 值
ggplot(data = df, aes(x = x, y = y)) +
stat_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() +
stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")), # adds R^2 and p-value
r.accuracy = 0.01,
p.accuracy = 0.001,
label.x = 0, label.y = 375, size = 4) +
stat_regline_equation(aes(label = ..eq.label..), # adds equation to linear regression
label.x = 0, label.y = 400, size = 4)
https://i.stack.imgur.com/d7fvx.jpg
当您有多个组时,也适用于 facet_wrap()
df$group <- rep(1:2,50)
ggplot(data = df, aes(x = x, y = y)) +
stat_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point() +
stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")),
r.accuracy = 0.01,
p.accuracy = 0.001,
label.x = 0, label.y = 375, size = 4) +
stat_regline_equation(aes(label = ..eq.label..),
label.x = 0, label.y = 400, size = 4) +
theme_bw() +
facet_wrap(~group)
https://i.stack.imgur.com/or08N.jpg
annotate
获得更好看的文本的评论在我的机器上是正确的。aes(
和相应的)
。aes
用于将数据帧变量映射到可视变量 - 这里不需要,因为只有一个实例,因此您可以将其全部放在主geom_text
调用中。我会将其编辑为答案。