答读者问~ggplot2画图添加拟合方程的R2并且在右上角添加星号表示显著性

一位读者在我的公众号 小明的数据分析笔记本 留言问到这个问题。

我记得之前分享过一篇文章 ggplot2绘图添加文本注释上下标问题,ggplot2画图如果添加文本注释可以用annotate()这个函数。
简单的小例子

library(extrafont)
fonts()
ggplot(df,aes(x=A,y=B,color=D))+
  geom_point(aes(shape=D),size=10)+
  theme_bw()+
  theme(legend.position = "none")+
  annotate(geom = "text",x=3,y=8.5,label="小明的数据分析笔记本",
           size=10,family="STKaiti")
image.png

如果要添加上标,annotate()函数label参数的写法

ggplot(df,aes(x=A,y=B,color=D))+
  geom_point(aes(shape=D),size=10)+
  theme_bw()+
  theme(legend.position = "none")+
  annotate(geom = "text",x=3,y=8.5,
           label="atop(小明的数据分析笔记本^'***')",
           parse=T,
           size=10)
image.png

添加拟合方程的R2的写法

ggplot(df,aes(x=A,y=B,color=D))+
  geom_point(size=5)+
  annotate("text",x=3,y=7.5,
           label="atop(R^2==0.9^'***')",
           parse=T,size=10)+
  theme_bw()+
  theme(legend.position="none")
image.png

欢迎大家关注我的公众号
小明的数据分析笔记本

留言讨论。
·

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容