# ggtrendline函数包，在ggplot中添加线性/非线性回归线、回归方程、R平方、P值

ggtrendline: an R package for adding trendline and confidence interval to ggplot

0. 链接

1. 在R或Rstudio软件中安装"ggtrendline" 包

• 从CRAN安装:

install.packages("ggtrendline")

• 从Github安装:

install.packages("devtools")

devtools::install_github("PhDMeiwp/ggtrendline@master", force = TRUE)

library(ggtrendline)

2. "ggtrendline" 包使用范例

library(ggplot2)

library(ggtrendline)

x <- c(1, 3, 6, 9,  13,   17)

y <- c(5, 8, 11, 13, 13.2, 13.5)

2.1 默认为"line2P"模型

ggtrendline(x, y, model = "line2P")

• 同时，默认输出line2P回归线，置信区间，回归方程，R方，P值。

2.2 添加geom_point()

ggtrendline(x, y, model = "line3P") +

geom_point(aes(x, y)) + theme_bw()

2.3 只添加CI线，不添加CI填充

ggtrendline(x, y, model = "log2P", CI.fill = NA) +

geom_point(aes(x, y))+ theme_classic()

2.4 设置回归线和geom_point()

ggtrendline(x, y, model = "exp2P", linecolor = "blue", linetype = 1, linewidth = 1) +

geom_point(aes(x, y), color = "blue", shape = 1, size = 3)

2.5 设置置信区间CI

ggtrendline(x, y, model = "exp3P", CI.level = 0.99,

CI.fill = "red", CI.alpha = 0.1, CI.color = NA, CI.lty = 2, CI.lwd = 1.5)

geom_point(aes(x, y))

2.6 单回归线+多组散点图

library(ggplot2)

library(ggtrendline)

data("iris")

x <- iris$Petal.Width y <- iris$Petal.Length

group <- iris\$Species

ggtrendline(x,y,"exp3P") + geom_point(aes(x,y,color=group))

3. 说明

3.1 R包描述

The 'ggtrendline' package is developed for adding trendline and confidence interval of linear or nonlinear regression model, and showing equation, R square, and P value  to 'ggplot' as simple as possible.

For a general overview of the methods used in this package, see Ritz and Streibig (2008) <doi:10.1007/978-0-387-09616-2> and Greenwell and Schubert Kabban (2014) <doi:10.32614/RJ-2014-009>.

3.2 ggtrendline函数

The built-in 'ggtrendline()' function includes the following models:

"line2P", formula as: y = ax + b;

"line3P", y = ax2 + bx + c;

"log2P" , y = aln(x) + b;

"exp2P", y = aebx

"exp3P", y = aebx + c;

"power2P", y = axb;

"power3P", y = axb + c.

3.3 stat_eq 和 stat_rrp 函数

The built-in 'stat_eq()' and 'stat_rrp()' functions can be used separately, i.e., not together with 'ggtrendline()' function.

To see more details, you can run the following R code if you have the "ggtrendline" package installed:

library(ggtrendline)

?ggtrendline

?stat_eq

?stat_rrp

4. 互动

- Bugs and feature requests can be filed to https://github.com/PhDMeiwp/ggtrendline/issues  .

- BTW, Pull requests are also welcome.

5. 致谢

We would like to express our special thanks to Uwe Ligges, Gregor Seyer, and CRAN team for their valuable comments to the 'ggtrendline' package.

https://blog.sciencenet.cn/blog-651374-1337028.html

## 全部精选博文导读

GMT+8, 2023-12-8 03:27