ggplot2点图+线性趋势+公式+R2+p值

ggplot2点图+线性趋势+公式+R2+p值
原创weixin_43948357 最后发布于2020-04-06 00:28:58 阅读数 13 收藏
展开
正文
先看效果。

R语言代码如下,

data("faithful")
library(ggplot2)
p <- ggplot(faithful,aes(x=eruptions,y=waiting)) + geom_point() + stat_smooth(method='lm',formula = y~x,colour='red')

model.lm<-lm(formula = waiting ~ eruptions, data = faithful)
#summary(model.lm)
#对于一元线性回归方程y=ax+b,Intercept是指的截距,x对应的是系数。

l <- list(a = as.numeric(format(coef(model.lm)[1], digits = 4)),
b = as.numeric(format(coef(model.lm)[2], digits = 4)),
r2 = format(summary(model.lm)$r.squared, digits = 4),
p = format(summary(model.lm)$coefficients[2,4], digits = 4))

eq <- substitute(italic(y) == a + b %.% italic(x)~","~italic(R)^2~"="~r2~","~italic(P)~"="~p, l)

p + geom_text(aes(x = 4, y = 50, label = as.character(as.expression(eq))), parse = TRUE)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
参考文献
【1】https://www.jianshu.com/p/d6a5eb41ecec
【2】https://www.zhihu.com/question/54776335/answer/141091529
————————————————
版权声明:本文为CSDN博主「weixin_43948357」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43948357/java/article/details/105336901

原文地址:https://www.cnblogs.com/xiaojikuaipao/p/12718157.html