Tidyverse自学笔记-ggplot2之图形标题

数据准备
本示例数据是自编数据,仅为练习所用,数据结构假设为,两个年份year(2020,2021),两个氮水平nitrogen(N1,N2),两个玉米品种variety(a,b)测定了5个试验指标(变量v1,v2,v3,v4,v5),每个处理3次重复block(1,2,3)。

library(tidyverse) # 调用tidyverse。
df <- read_csv(file = "df.csv") # 导入数据。文档在工作目录下,所以直接给文件名导入。
df # 查看数据。
## # A tibble: 24 × 9
##     year nitrogen variety block    v1    v2    v3    v4    v5
##    <dbl> <chr>    <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1  2020 N1       a           1  1.26  2.14   0.4   5    3.25
##  2  2020 N1       a           2  1.2   2.9    0.1   5.3  1.27
##  3  2020 N1       a           3  1.3   3      0.3   5.6  2.24
##  4  2020 N1       b           1  1.08  1.72   1.8   2.8  1   
##  5  2020 N1       b           2  1.05  1.65   1.7   2.5  3.12
##  6  2020 N1       b           3  1.15  1.35   1.5   3.1  4.57
##  7  2020 N2       a           1  1.32  3.78   1.6   6    5.85
##  8  2020 N2       a           2  1.28  4.32   1.4   6.1  6.48
##  9  2020 N2       a           3  1.35  3.95   1.3   6.2  7.21
## 10  2020 N2       b           1  1.33  3.47   2.8   4.1  6.56
## # … with 14 more rows
## # ℹ Use `print(n = ...)` to see more rows

7.2.3 图形标题

ggtitle() 添加主副标题。

ggplot(df, aes(v3, v4)) + geom_point() + ggtitle("This is just an example\n主标题", subtitle = "这是副标题") # 添加主副标题。
image.png

可以看出标题默认是在图形左上角的,要调整位置可配合theme函数完成,其中的hjust是水平方向的调整参数,vjust是垂直方向的调整参数。

ggplot(df, aes(v3, v4)) + geom_point() + ggtitle("This is just an example\n主标题", subtitle = "这是副标题") + theme(plot.title = element_text(color = 'red', size = 20, hjust = 0.5)) # 标题居中。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + ggtitle("This is just an example\n主标题", subtitle = "这是副标题") + theme(plot.title = element_text(color = 'red', size = 20, hjust=0.5, vjust = -15)) # 通过vjust参数将标题调整到图形内部。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + annotate("text", x=1.5, y = 7, label = "This is just an example") # 将标题直接移动到图内。
image.png

labs() 添加标题。

ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + labs(title = "这是主标题", subtitle = "这是副标题", caption = "这是图注", x = "这是x轴", y = "这是y轴", color = "图例标题") # labs设置了标签,title用于指定主标题,subtitle用于指定副标题,caption用于指定图注,x和y分别用于指定x和y轴标题,color用于指定图例标题。
image.png
ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + labs(title = "这是主标题", subtitle = "这是副标题", caption = "这是图注", x = "这是x轴", y = "这是y轴", color = "图例标题") + theme(plot.title = element_text(hjust = 0.5)) # 调整标题位置。
image.png

坐标轴标题

可以用xlab或ylab分别设置,也可以在labs中指定。

ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + xlab("this is x axis") + ylab("this is y axis") # 指定轴标题。
image.png
ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + labs(x="this is x axis", y="this is y axis") # 同上。
image.png
ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + labs(x="this is x axis", y="this is y axis") + theme(axis.title.x = element_text(hjust = 1), axis.title.y = element_text(hjust = 0)) # 坐标轴标题位置调整。
image.png
ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + labs(x="this is x axis", y="this is y axis") + theme(axis.title.x = element_text(margin = margin(2,1,0,1,"cm")), axis.title.y = element_text(hjust = 0)) # x轴标题与坐标轴距离调整。
image.png

轴标题含特殊字符

ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + xlab(bquote("this is x axis" (kg *hm^-2))) # 坐标轴标题含特殊字符。
image.png
ggplot(df, aes(v3, v4, color = nitrogen)) + geom_point() + xlab(expression(Production~rate~""~mu~moles~NO[3]^{-1}-N~Kg^{-1})) # 再一个例子。
image.png

参考资料

1. R语言编程—基于 tidyverse,张敬信,人民邮电出版社,2022.
2. R语言教程,李东风,https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/index.html
3. 《R数据科学》,人民邮电出版社,2018.
4. R Graphics Cookbook, 2nd edition,https://r-graphics.org/index.html

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

推荐阅读更多精彩内容