码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • R语言ggplot2 title设置(main, axis 和 legend titles)


    R语言ggplot2 title设置(main, axis 和 legend titles)

          • 1. ggplot2中添加title函数
          • 2. 实际应用
            • (1)添加title、xlab和ylab
            • (2)修改legend名字
            • (3)修改title的字体,位置,颜色等
            • (4)删除xlab和ylab

    1. ggplot2中添加title函数
    ggtitle(label) # for the main title,主题目
    xlab(label) # for the x axis label, xlab
    ylab(label) # for the y axis label, ylab
    labs(...) # for the main title, axis labels and legend titles,可以同时设定多个lab和tittle
    
    • 1
    • 2
    • 3
    • 4
    2. 实际应用
    (1)添加title、xlab和ylab
    ToothGrowth$dose <- as.factor(ToothGrowth$dose)
    library(ggplot2)
    p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
    ## 方法1:
    p + ggtitle("Plot of length \n by dose") +
      xlab("Dose (mg)") + ylab("Teeth length")
    
    ## 方法2:
    p +labs(title="Plot of length \n by dose",
            x ="Dose (mg)", y = "Teeth length")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    (2)修改legend名字
    # Default plot
    p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+
      geom_boxplot()
    p
    # Modify legend titles
    p + labs(fill = "Dose (mg)")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    (3)修改title的字体,位置,颜色等

    参数,其中hjust和vjust可以调节位置, anglexlab和ylab调节角度,size可以调节label大小:

    family : font family
    face : font face. Possible values are “plain”, “italic”, “bold” and “bold.italic”
    colour : text color
    size : text size in pts
    hjust : horizontal justification (in [0, 1])
    vjust : vertical justification (in [0, 1])
    lineheight : line height. In multi-line text, the lineheight argument is used to change the spacing between lines.
    color : an alias for colour
    angle: angle
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用:

    # Default plot
    p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
      ggtitle("Plot of length \n by dose") +
      xlab("Dose (mg)") + ylab("Teeth length")
    p
    # Change the color, the size and the face of
    # the main title, x and y axis labels
    p + theme(
    plot.title = element_text(color="red", size=14, face="bold.italic"),
    axis.title.x = element_text(color="blue", size=14, face="bold"),
    axis.title.y = element_text(color="#993333", size=14, face="bold")
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    此外,修改坐标轴的angle也是相似的构造:

    require(ggplot2)
    ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point()  + theme(axis.text.x = element_text(angle=90))
    ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point()  + theme(axis.text.y = element_text(angle=90))
    
    • 1
    • 2
    • 3
    (4)删除xlab和ylab
    # Hide the main title and axis titles
    p + theme(
      plot.title = element_blank(),
      axis.title.x = element_blank(),
      axis.title.y = element_blank())
    
    • 1
    • 2
    • 3
    • 4
    • 5

    总之,一次性设定ggplot相关title的话, labs(title=" ", x=" ",y=" ")即可,修改需要使用后面的theme(axis.text.x = element_text(angle=90)),类似这种设定。

    翻译来源:
    http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles

  • 相关阅读:
    # Kafka_深入探秘者(1):初识 kafka
    深度学习基础学习-1x1卷积核的作用(CNN中)
    福建厦门航空飞机发动机零部件检测3D测量尺寸偏差比对-CASAIM中科广电
    2023火锅展/江西火锅食材展/中国火锅展/火锅用品展
    基于JAVA+SpringBoot+Mybatis+MYSQL的车辆信息管理系统
    opencv_5_图像像素的算术操作
    你必须知道的4种 Redis 集群方案及优缺点对比
    一个月赚够一年钱的“服装博览会”,堪称商户老板的噩梦!
    【C++】AVL树的4中旋转调整
    玩转Vue3之shallowRef和shallowReactive
  • 原文地址:https://blog.csdn.net/cfc424/article/details/126708905
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号