匠人府分享 http://blog.sciencenet.cn/u/meiweipingg

博文

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

已有 6328 次阅读 2022-5-4 21:52 |个人分类:R语言|系统分类:科研笔记


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


CRAN.png


0. 链接


链接: https://cran.r-project.org/package=ggtrendline 和 https://github.com/PhDMeiwp/ggtrendline 


 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")  


  • 默认输出统计结果,包括:回归方程的各项参数,R方,调整R方,P值,样本数N,赤池信息量AIC,小样本AIC值即AICc,贝叶斯信息量BIC,以及残差平方和RSS。


summary.png


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

line2P.png


2.2 添加geom_point()


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

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


line3P.png


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


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

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

log2P.png


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)  


exp2P.png

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)) 


exp3P.png


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))


group.png


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

上一篇:拟合模型比较之AIC, AICc, BIC(附相关R函数包)
收藏 IP: 111.58.209.*| 热度|

1 杨正瓴

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-28 21:43

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部