使用ggplot2包绘制时,为了更直观地向用户显示报表的内容和外观,须要使用geom_text()函数添加文本说明,使用annotate()添加注释,并经过theme()来调整非数据的外观。html
向图中增长文本内容,能够使用标度来实现,特殊的标度函数是:labs()、xlab()、ylab()、ggtitle(),分别修改标签(title、x标签、y标签)、x标签、y标签和标题,也能够使用geom_text() 和 geom_label() 来实现,前者仅仅是绘制文本,后者会在文本的后面添加矩形背景。使用geom_text()函数增长文本图层:app
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)
参数注释:ide
geom_text()能够识别的aes参数:函数
使用annotate()函数增长注释图层,该图层不会映射到数据框对象,而是经过向量来传递值,这对于向图层中添加小注释(例如文本标签),添加注释的图层只能局限在绘图区域(plot)范围以内。字体
annotate(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL, ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ..., na.rm = FALSE)
参数注释:spa
当geom参数值是text时,表示添加的是文本注释,使用lable来设置文本内容:debug
x=x_start,y=y_start,geom="text", label = "italic(R) ^ 2 == 0.75", parse = TRUE
主题(Theme)用于控制全部的非数据外观,函数theme()可以控制plot中的全部axis、legend、panel、plot和strip等可视化元素的属性。code
theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y, axis.title.y.left, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left, axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, axis.ticks.length, axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right, legend.background, legend.margin, legend.spacing, legend.spacing.x, legend.spacing.y, legend.key, legend.key.size, legend.key.height, legend.key.width, legend.text, legend.text.align, legend.title, legend.title.align, legend.position, legend.direction, legend.justification, legend.box, legend.box.just, legend.box.margin, legend.box.background, legend.box.spacing, panel.background, panel.border, panel.spacing, panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.subtitle, plot.caption, plot.tag, plot.tag.position, plot.margin, strip.background, strip.background.x, strip.background.y, strip.placement, strip.text, strip.text.x, strip.text.y, strip.switch.pad.grid, strip.switch.pad.wrap, ..., complete = FALSE, validate = TRUE)
在修改特定的主题元素时,能够使用如下函数,用于修改元素边缘、矩形框、线条和文本:htm
margin(t = 0, r = 0, b = 0, l = 0, unit = "pt")
element_blank()
element_rect(fill = NULL, colour = NULL, size = NULL, linetype = NULL, color = NULL, inherit.blank = FALSE) element_line(colour = NULL, size = NULL, linetype = NULL, lineend = NULL, color = NULL, arrow = NULL, inherit.blank = FALSE) element_text(family = NULL, face = NULL, colour = NULL, size = NULL, hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL, color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE)
参数注释:对象
因为Theme()中的美学属性太多,ggplot2预约义了几个主题:
参考文档: