参考:《R数据可视化手册》
文本者,ggplot2中的文字也。
包括:1、坐标轴标签 2、标题 3、手动添加文本 4、映射数据的文本等
一、修改坐标轴标签外观: 使用theme(axis.title.x = element_text())
library(ggplot2)
library(gcookbook) # Load gcookbook for the heightweight data set
# Base plot
hw_plot <- ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
geom_point()
# Controlling appearance of theme items
hw_plot +
theme(axis.title.x = element_text(
size = 16, lineheight = .9,
family = "Times", face = "bold.italic", colour = "red"
))
# size:坐标轴标签字体大小,lineheight: 标签行间距的倍数,family:字体,face:字体外形(粗斜体等)
![f2e689503d4f35685fe405179d137f9d.png](https://i-blog.csdnimg.cn/blog_migrate/9b7283c68a32aa588808d43011113445.png)
二、标题外观 :theme(plot.title = element_text())
hw_plot +
ggtitle("I'm new title") +
theme(plot.title = element_text(
size = rel(1.5), lineheight = .9,
family = "Times", face = "bold.italic", colour = "red"
))
![8591fb461eecde85b578f81595d08f3f.png](https://i-blog.csdnimg.cn/blog_migrate/352f9830fb46a0228161e9a042833aca.png)
三、修改通过annotate()添加的文本标签的外观:annotate("text",,,)
hw_plot +
annotate("text", x = 15, y = 53, label = "I'm text",
size = 7, family = "Times", fontface = "bold.italic", colour = "red")
![db97990a0822ffe7f0972b3e16fa6329.png](https://i-blog.csdnimg.cn/blog_migrate/ff56318fc51b9a6dea712b6d12b8cc34.png)
四、添加映射数据点值大小的标签的外观: geom_text(aes(label = 要映射的变量名),,,)
hw_plot +
geom_text(aes(label = weightLb), size = 4, family = "Times", colour = "red")
![edccae2639b8741ed043b9b4910cc115.png](https://i-blog.csdnimg.cn/blog_migrate/4bd213e0a4a8e9a141762c98b77159ec.jpeg)
五、theme_elements和text_geoms之争
theme_elements控制的文本:不直接映射数据的文本,如标题、坐标轴标签、图例。
text_geoms控制文本:映射数据的文本,本身是图(x、y轴线内部意义上的图)的一部分。
二者参数设置:查询网络或《R数据可视化手册》第9.2节等