码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • R语言ggplot2添加单个文本或多个文本


    R语言ggplot2添加单个文本或多个文本

          • 1. 图中添加单个文本(annotate函数,grid.text函数)和公式
          • 2. 散点图全部添加文字label

    1. 图中添加单个文本(annotate函数,grid.text函数)和公式

    比如添加TEST字符:

    p1 <- ggplot(mtcars, aes(x=mpg, y=wt)) +
      geom_point(color = "grey20",size = 1, alpha = 0.8)
    p1 + 
      annotate("text", x = 20 , y = 4,label = "TEST",colour="red") + 
      annotate("text", x = 20 , y = 3,label = "TEST2",colour="blue") + theme_bw()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    这里关注到annotate函数,很好用,还有其他应用:

    p1 <- ggplot(mtcars, aes(x=mpg, y=wt)) +
      geom_point(color = "grey20",size = 1, alpha = 0.8)
    
    p1 + 
      annotate("text", x = -4 , y = 1.2,label = "TEST",colour="red") + 
      annotate("text", x = -4 , y = 0.8,label = "TEST2",colour="blue") 
    
    
    p1
    grid.text("text1", x = 0.2, y = 0.8)
    grid.text("text2", x = 0.2, y = 0.8)
    
    ## annotate examples
    p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
    p + annotate("text", x = 4, y = 25, label = "Some text")
    p + annotate("text", x = 2:5, y = 25, label = "Some text")
    p + annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21,
                 alpha = .2)
    p + annotate("segment", x = 2.5, xend = 4, y = 15, yend = 25,
                 colour = "blue")
    p + annotate("pointrange", x = 3.5, y = 20, ymin = 12, ymax = 28,
                 colour = "red", size = 1.5)
    
    p + annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2"))
    
    p + annotate("text", x = 4, y = 25, label = "italic(R) ^ 2 == 0.75",
                 parse = TRUE)
    p + annotate("text", x = 4, y = 25,
                 label = "paste(italic(R) ^ 2, \" = .75\")", parse = TRUE)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    添加公式:

    require(gridExtra)
    require(ggplot2)
    p=ggplot(data.frame(x=c(-3,3)), aes(x=x)) + stat_function(fun=dnorm)
    p1=p + annotate("text", x=2, y=0.3, parse=T, label="y==frac(1, sqrt(2*pi)) * e^{-x^2 / 2}")
    p2=p + annotate("text", x=2, y=0.3, parse=T, label="frac(1, sqrt(2*pi)) * e^{-x^2 / 2}")
    grid.arrange(p1,p2, nrow=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    对于grid.text()函数,需要加载grid包,使用体验不如annotate:

    p1 <- ggplot(mtcars, aes(x=mpg, y=wt)) +
      geom_point(color = "grey20",size = 1, alpha = 0.8)
    
    p1
    grid.text("text1", x = 0.2, y = 0.5)
    grid.text("text2", x = 0.2, y = 0.8,gp=gpar(col="red"))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    详细的功能可以查看函数帮助 ?grid.text

    2. 散点图全部添加文字label

    散点图有需要添加label的需求时候,可以使用ggplot中的geom_text;
    或者使用ggrepel包中的,可以防止label重叠:

    geom_label_repel()
    geom_text_repel()
    
    • 1
    • 2

    参考:
    http://www.sthda.com/english/wiki/ggplot2-texts-add-text-annotations-to-a-graph-in-r-software
    https://stackoverflow.com/questions/66458560/how-to-add-single-annotation-to-overall-ggplot-plot-and-not-to-each-facet

  • 相关阅读:
    机器学习:随机森林
    用滑动条做调色板---cv2.getTrackbarPos(),cv2.creatTrackbar()
    【Typescript重点】泛型的使用
    「Verilog学习笔记」边沿检测
    【羚珑AI智绘营】分分钟带你拿捏SD中的色彩控制
    JS-水果库存记录实现全选全不选功能
    基于PID控制的四旋翼飞行器仿真(Matlab代码实现)
    【原型与原型链】初识原型与原型链~
    python金融分析小知识(38)——Jupyter Notebook更改文件路径
    SAP 刷新Fiori Apps缓存的方法(解决修改CDS后Fiori无法重载新配置)
  • 原文地址:https://blog.csdn.net/cfc424/article/details/126759163
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号