科学网

 找回密码
  注册

tag 标签: geom_smooth

相关帖子

版块 作者 回复/查看 最后发表

没有相关内容

相关日志

R语言笔记——ggplot2画回归曲线,添加方程或P值
Hunshandake 2020-1-2 15:35
library(ggplot2) library(dplyr) # 加载 dplyr 包 library(ggpmisc) # 加载 ggpmisc 包 library(RColorBrewer) library(ggpubr) # 载入数据,计算均值和 se caomuxi-read.csv(E:/R/Rfiles/data.csv) windowsFonts(SH = windowsFont(Times New Roman)) #第一种方案,做点状图,加回归线,添加回归方程和R 2 a- ggplot(data=data,aes(x=Fert, y=Hight))+ geom_point(aes(color=treatment),size = 3)+ # 设置分组颜色和点的大小 geom_smooth(method = lm,linetype=3,se=FALSE,colour=black,span=0.8)+ # 添加回归曲线, se 取消置信空间, linetype 设置线型 stat_poly_eq(aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~~')), formula = y ~ x, parse = T,family = SH) + # 添加回归方程和 R 2 scale_x_continuous()+ scale_y_continuous(expand = c(0, 0),# 设定 x 轴和 y 轴的交叉点 name =Height (cm),# 设定 y 轴标题 breaks=seq(0,50,10),# 设定 Y 轴的数据间隔 limits = c(10,50) # 设定 Y 轴的数据上下限 )+ scale_color_brewer(palette = YlOrRd)+ theme() a 出图如下: #第二种方案,做点状图,加回归线,添加R 2 和P值 a- ggplot(data=data,aes(x=Fert, y=Hight))+ geom_point(aes(color=treatment),size = 3)+ # 设置分组颜色和点的大小 geom_smooth(method = lm,linetype=3,se=FALSE,colour=black,span=0.8)+ # 添加回归曲线, se 取消置信空间, linetype 设置线型 stat_fit_glance(method = 'lm', method.args = list(formula = y ~ x), mapping = aes(label = sprintf('R^2~=~%.3f~~italic(P)~=~%.2g', stat(r.squared), stat(p.value))), parse = TRUE,label.x = 0.95,label.y = 0.95,family = SH)+ # 方案 2 ,仅添加 R 2 和 P 值, label.x 和 label.y 设置文字位置。 scale_x_continuous()+ scale_y_continuous(expand = c(0, 0),# 设定 x 轴和 y 轴的交叉点 name =Height (cm),# 设定 y 轴标题 breaks=seq(0,50,10),# 设定 Y 轴的数据间隔 limits = c(10,50) # 设定 Y 轴的数据上下限 )+ scale_color_brewer(palette = YlOrRd)+ theme() a 出图如下:
个人分类: 软件使用|22916 次阅读|0 个评论

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

GMT+8, 2024-6-2 22:10

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部