参考:《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:字体外形(粗斜体等)
二、标题外观 :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"
))
三、修改通过annotate()添加的文本标签的外观:annotate("text",,,)
hw_plot +
annotate("text", x = 15, y = 53, label = "I'm text",
size = 7, family = "Times", fontface = "bold.italic", colour = "red")
四、添加映射数据点值大小的标签的外观: geom_text(aes(label = 要映射的变量名),,,)
hw_plot +
geom_text(aes(label = weightLb), size = 4, family = "Times", colour = "red")
五、theme_elements和text_geoms之争
theme_elements控制的文本:不直接映射数据的文本,如标题、坐标轴标签、图例。
text_geoms控制文本:映射数据的文本,本身是图(x、y轴线内部意义上的图)的一部分。
二者参数设置:查询网络或《R数据可视化手册》第9.2节等