7.2.4 图形注释
文本注释
指定位置的文本
geom_text()函数
geom_text(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
mapping:通过aes()创建的一系列美学映射。
常见映射参数包括x, y,data,label,family,color,size,angle,fontface,vjust,hjust,nudge_x,nudge_y,parse,check_overlap等。
x和y指定注释的位置;
data指定用于注释的数据集,若不指定,默认使用全局数据集;
label指定注释内容;family指定字体;color指定颜色;size指定大小;angle指定角度;fontface指定字体样式;
vjust纵向偏移调节,hjust横向偏移调节;nudge_x和nudge_y用于调节注释偏移量,与vjust和hjust参数不同的是,它的单位和对应坐标轴的刻度相同;
parse指定为TRUE时,可添加公式等内容;
check_overlap为逻辑型参数,若设为TRUE,注释避免重叠。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_text(x = 1.4, y = 4, label = "Group 2", alpha = .1) # 添加文本注释。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_text(aes(x = 1.4, y = 4, label ="italic(y) == frac(x,y)%*%sqrt(x)"), parse = T, color = "red", size = 6, alpha = 0.05, angle = 30, family = "serif", fontface = "bold.italic") # 各参数设置尝试。
annotate()函数
annotate(
geom,
x = NULL,
y = NULL,
xmin = NULL,
xmax = NULL,
ymin = NULL,
ymax = NULL,
xend = NULL,
yend = NULL,
...,
na.rm = FALSE
)
geom指定几何对象;x和y指定注释位置;xmin等是用于当几何对象为线段或几何图形时指定几何对象的边界。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + annotate("text", x = 1.4, y = 4, parse = TRUE, size = 4, label = "'Function: ' * y==frac(1, sqrt(2*pi)) * e^{-x^2/2}") # 添加公式注释。
geom_label()函数
geom_label(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"),
label.size = 0.25,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
参数多与geom_text参数相同,以下为不同参数:
- label.padding:标签文本与外框的距离大小;
- label.r:外框圆角的半径;
- label.size:外框线条的尺寸,单位为毫米(mm)。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_label(aes(x=1.4,y=4,label="注释"), size = 4, color = "black", label.padding = unit(0.1, "lines"), label.r = unit(0.05, "lines"), label.size = 0.1) # geom_label添加注释。
对图形元素添加注释
geom_text()函数
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_text(aes(label = variety), size = 4) # 添加图形元素注释。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_text(aes(label = variety), size = 4, nudge_x = 0.02, nudge_y = 0.2, check_overlap = T) # 参数调整。nudge_x or y调整了注释偏移量,check_overlap设定注释不重叠。
geom_label()函数
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_label(aes(label = variety), size = 4, nudge_x = 0.02) # 添加图形元素注释。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + geom_label(aes(label = v5), size = 4, label.padding = unit(0.1, "lines"), label.r = unit(0.05, "lines"), label.size = 0.1, nudge_x = 0.02) # 参数调整。
ggrepel包实现图形元素注释。
library(ggrepel) # 调用ggrepel包,此包用于添加标注。
ggplot(df, aes(v1, v5, color = nitrogen)) + geom_point() + labs(title = "这是散点图", subtitle = "这是副标题", caption = "这是图注", x = "这是x轴", y = "这是y轴", color = "N") + geom_text_repel(aes(label = v1)) # 添加图形元素注释。
参考资料
*1. R语言编程—基于 tidyverse,张敬信,人民邮电出版社(待出版),2022.
- R语言教程,李东风,https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/index.html
- 《R数据科学》,人民邮电出版社,2018.
- R Graphics Cookbook, 2nd edition,https://r-graphics.org/index.html
5.ggplot2 | 注释函数ggplot2 | 注释函数,https://zhuanlan.zhihu.com/p/404747239*