MYa*_*208 207 r ggplot2 ggpmisc
我想知道如何添加回归线方程和R ^ 2 ggplot.我的代码是
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
Run Code Online (Sandbox Code Playgroud)
任何帮助将受到高度赞赏.
Ram*_*ath 218
这是一个解决方案
# 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)
Run Code Online (Sandbox Code Playgroud)
编辑.我从我选择此代码的地方找到了源代码.这是ggplot2 google群组中原始帖子的链接

Ped*_*alo 114
我stat_poly_eq()在我的包ggpmisc中包含了一个统计数据,允许这个答案:
library(ggplot2)
library(ggpmisc)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
Run Code Online (Sandbox Code Playgroud)
此统计数据适用于任何没有缺失项的多项式,并且希望具有足够的灵活性以通常有用.R ^ 2或经调整的R ^ 2标记可与任何配有lm()的模型公式一起使用.作为一个ggplot统计数据,它的行为与团队和方面一样.
'ggpmisc'包可以通过CRAN获得.
版本0.2.6刚刚被CRAN接受.
它涉及@shabbychef和@ MYaseen208的评论.
@ MYaseen208这显示了如何添加帽子.
library(ggplot2)
library(ggpmisc)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
Run Code Online (Sandbox Code Playgroud)
@shabbychef现在可以将方程中的变量与用于轴标签的变量相匹配.要更换X与说ž和ÿ与^ h,应当使用:
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
eq.with.lhs = "italic(h)~`=`~",
eq.x.rhs = "~italic(z)",
aes(label = ..eq.label..),
parse = TRUE) +
labs(x = expression(italic(z)), y = expression(italic(h))) +
geom_point()
p
Run Code Online (Sandbox Code Playgroud)
作为这些正常的R解析表达式,希腊字母现在也可以在等式的lhs和rhs中使用.
[2017-03-08] @elarry编辑以更精确地解决原始问题,显示如何在等式和R2标签之间添加逗号.
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
parse = TRUE) +
geom_point()
p
Run Code Online (Sandbox Code Playgroud)
kda*_*ria 98
我更改了几行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)
Run Code Online (Sandbox Code Playgroud)

我使用@ Ramnath答案中的代码来格式化等式.该stat_smooth_func功能不是很强大,但它不应该很难玩.
https://gist.github.com/kdauria/524eade46135f6348140.ggplot2如果出现错误,请尝试更新.
Jay*_*den 72
我已经将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));
}
Run Code Online (Sandbox Code Playgroud)
用法将变为:
p1 = p + geom_text(aes(x = 25, y = 300, label = lm_eqn(lm(y ~ x, df))), parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
小智 19
这里给大家最简单的代码
注意:显示的是 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
Run Code Online (Sandbox Code Playgroud)
zx8*_*754 16
使用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)
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)
真的很喜欢@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')
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不适用于facet_wrap或facet_grid.
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
Run Code Online (Sandbox Code Playgroud)
现在我们需要将文本添加到图中:
p +
geom_text(x = 20, y = 300,
label = get_formula(model),
color = 'red') +
geom_text(x = 20, y = 285,
label = r_squared,
color = 'blue')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
226446 次 |
| 最近记录: |