美文网首页ggplot2 绘图学习
答读者问~ggplot2画图添加拟合方程的R2并且在右上角添加星

答读者问~ggplot2画图添加拟合方程的R2并且在右上角添加星

作者: 小明的数据分析笔记本 | 来源:发表于2020-11-20 12:29 被阅读0次

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

我记得之前分享过一篇文章 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

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

留言讨论。
·

相关文章

网友评论

    本文标题:答读者问~ggplot2画图添加拟合方程的R2并且在右上角添加星

    本文链接:https://www.haomeiwen.com/subject/ghsdiktx.html