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

7.2.2 坐标轴标签设置

删除刻度线和标签

theme(axis.title.x or y = element_blank()) 删除轴标签。
xlab(NULL) 删除x轴标签

ggplot(df, aes(v3, v4)) + geom_point() # 基础散点图。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.title.x = element_blank()) # 删除x轴标签。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.title.y = element_blank()) # 删除y轴标签。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + xlab(NULL) # 删除x轴标签。
image.png

theme(axis.ticks = element_blank()) 删除刻度线。
theme(axis.text.x or y = element_blank()) 删除轴刻度标签。

ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.ticks = element_blank()) # 删除轴刻度线。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.ticks = element_blank(), axis.text = element_blank()) # 删除轴刻度线并删除刻度标签。
image.png

修改刻度标签

连续变量刻度标签设置

scale_x or y_continuous(name, breaks, labels, limits, trans)
name指定x或y轴的标签,若为NULL,不显示轴标签;
breaks指定各个刻度的位置,若为NULL,不显示刻度;
labels指定各个刻度的标签;
limits指定刻度的范围;
trans指定坐标轴值转换,如可以为log10。

ggplot(df, aes(v3, v4)) + geom_point() # 基础散点图。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + scale_x_continuous(name = "This is x axis", breaks = seq(0, 4, by = 1), labels = c("一", "二", "三", "四", "五"), limits = c(0, 4)) + scale_y_continuous(name = "This is y axis", breaks = seq(2, 8, by = 1), labels = c("a", "b", "c", "d", "e", "f", "g"), limits = c(2, 8))  # 修改散点图y轴刻度和标签。
image.png

离散型变量刻度标签设置

scale_x or y_discrete():修改离散变量坐标轴的标签。

ggplot(df, aes(nitrogen, v5)) + geom_bar(stat = "identity", position = "dodge") # 基础柱状图。
image.png
ggplot(df, aes(nitrogen, v5)) + geom_bar(stat = "identity", position = "dodge") + scale_x_discrete(labels = c("N1" = "Nitrogen rate1", "N2" = "Nitrogen rate2")) # 修改离散型变量x坐标轴标签。
image.png

从以上可以看出,在scale_x or y_continous和discrete中将一些参数的值赋予NULL,也可以起到删除的效果。

日期型刻度标签设置

scale_x_date()设置日期刻度,参数date_breaks设置刻度间隔,date_labels设置标签的日期格式;借助 scales包中的函数设置特殊格式,比如百分数(percent)、科学计数法 (scientific)、美元格式 (dollar)等。

economics_long # 内置数据集。
## # A tibble: 2,870 × 4
##    date       variable value  value01
##    <date>     <chr>    <dbl>    <dbl>
##  1 1967-07-01 pce       507. 0       
##  2 1967-08-01 pce       510. 0.000265
##  3 1967-09-01 pce       516. 0.000762
##  4 1967-10-01 pce       512. 0.000471
##  5 1967-11-01 pce       517. 0.000916
##  6 1967-12-01 pce       525. 0.00157 
##  7 1968-01-01 pce       531. 0.00207 
##  8 1968-02-01 pce       534. 0.00230 
##  9 1968-03-01 pce       544. 0.00322 
## 10 1968-04-01 pce       544  0.00319 
## # … with 2,860 more rows
ggplot(head(economics_long, 20), aes(x = date, y = value)) + geom_line() # 基础线图。
image.png
ggplot(head(economics_long, 20), aes(x = date, y = value)) + geom_line() + scale_x_date(date_breaks = "5 months", date_labels = "%b%Y") # 改变x轴刻度为每6个月间隔,标签为月年格式。
image.png

轴标签美学设置

轴标签美学
axis.title() 更改x和y轴外观。
axis.title.x() 更改x轴外观。
axis.title.y() 更改y轴外观。

ggplot(df, aes(v3, v4)) + geom_point() + xlab("this is x") + theme(axis.title = element_text(colour = "red", size = 15)) # 更改x轴和y轴外观。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.title.x = element_text(face = "italic", size = 15, colour = "blue")) # 更改x轴标签字体样式,大小,颜色。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.title.y = element_text(family = "serif", face = "bold", size = 20, colour = "brown")) # 更改x轴标签字体,字体样式,大小,颜色。
image.png

刻度标签美学

theme(axis.text = element_text())中axis.text 和/或从属元素 axis.text.x 和 axis.text.y 来更改轴文本的外观。
element_text中参数说明。
angle:指定刻度线标签旋转角度;
hjust:设置水平对齐方式(左,中,右);
vjust:设置垂直对齐方式(顶部,中间,底部);
colour:设置字体颜色;
size:设置字体大小;
family:设置字体;windowsFonts()可查看windows字体;
face:设置字体样式,粗体bold,斜体italic;

ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.text = element_text(colour = "red")) # 改变轴标签字颜色为红色。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.text = element_text(colour = "red"), axis.text.x = element_text(colour = "blue", size = 20)) # 单独修改x轴标签字体颜色为蓝色。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.text = element_text(colour = "red"), axis.text.x = element_text(angle = 90, hjust = 0.1, vjust = 0.5)) # x轴刻度标签文本旋转90度,hjust用于设置水平对齐方式(左,中,右),vjust用于设置垂直对齐方式(顶部,中间,底部)。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.text = element_text(family = "serif")) # 设置字体为新罗马。
image.png
ggplot(df, aes(v3, v4)) + geom_point() + theme(axis.text = element_text(face = "italic")) # 设置刻度线标签字体为斜体。
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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容