几何对象注释
annotate函数可用于添加任何类型的几何对象。
参数geom用于指定对象,text是文本,mtext是轴标签,segments是短线,arrows是箭头,rect是矩形,curve是曲线。
ggplot(df, aes(v3, v4)) + geom_point() # 绘制基础图形。
ggplot(df, aes(v3, v4)) + geom_point() + annotate(geom = "text", x = 2, y = 6, label = "注释") # 在指定点添加文本注释。
ggplot(df, aes(v3, v4)) + geom_point() + annotate("text", x = 1:2, y = 5:6, label = "Some text") # 指定的多个点同时添加文本注释。
ggplot(df, aes(v3, v4)) + geom_point() + annotate("rect", xmin = 1, xmax = 2, ymin = 3, ymax = 5,alpha = .2) # 添加矩形。
ggplot(df, aes(v3, v4)) + geom_point() + geom_rect(aes(xmin = 0, xmax = 2, ymin = 3, ymax = 6), alpha = 0.02, data = df) # 通过geom_rect函数添加矩形。
ggplot(df, aes(v3, v4)) + geom_point() + annotate("segment", x = 1, xend = 2, y = 5, yend = 6,colour = "blue") # 添加线段。
ggplot(df, aes(v3, v4)) + geom_point() + annotate("segment", x = 1, xend = 2, y = 5, yend = 6, colour = "red", arrow = arrow()) # 添加箭头。
ggplot(df, aes(v3, v4)) + geom_point() + annotate("pointrange", x = 2, y = 5, ymin = 5, ymax = 6, colour = "red", size = 1.5) # 添加点线。
ggplot(df, aes(v3, v4)) + geom_point() + annotate("curve", x = 1, y = 5, xend = 1.6, yend = 5.9, curvature = 0.5, colour = "red", size = 1, arrow = arrow(length = unit(2, "mm"))) + annotate(geom = "text", x = 0.7, y = 5, label = "就是这个点", color = "blue") # 添加带箭头曲线和文本标注。
参考资料
- 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