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


    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

  • 相关阅读:
    UNIX网络编程卷一 学习笔记 第三十一章 流
    【面试高高手】—— Java垃圾回收
    WEB自动化_PO模式设计原理和设计规范
    单调栈介绍和使用
    Python蓝桥杯---2的次幂表示
    NodeJS校园快递智能互助平台-计算机毕业设计源码58554
    [英语学习] 理解实例:晶振;时钟
    IO多路复用
    你不一定全部知道的16种进程注入方法和注入工具(C语言版)
    Codeforces暑期训练周报(8.15~8.21)
  • 原文地址:https://blog.csdn.net/cfc424/article/details/126708905