R绘图 第十篇:绘制文本、注释和主题(ggplot2)

使用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

  • mapping:映射,使用aes()设置映射
  • stat:统计转换
  • position:位置调整
  • check_overlap:默认值是 FALSE,若是是TRUE,新增的文本不会覆盖同一图层中的先前文本。
  • parse:默认值是FALSE,不对文本内容进行解析。
  • show.legend:是否显示该文本图层的图例(legend)
  • nudge_x,nudge_y:把文本作小幅移动的距离

geom_text()能够识别的aes参数:函数

  • x
  • y
  • label:显示的文本
  • alpha:文本重叠部分的透明度
  • angle:文本旋转的角度
  • colour:文本的前景色
  • family:字体名称
  • fontface:字体类型,有效值是:"plain", "bold", "italic", "bold.italic"
  • group:分组
  • hjust、vjust:文本的水平和垂直调整距离,取值范围是0-1,0表明right/bottom,1表明left/top。
  • lineheight:
  • size:字体的大小

 二,注释图层

使用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、pointrange、segment和rect,
  • x、y:用于指定图层的开始位置

当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)

参数注释:对象

  • fill:填充色
  • color:前景色
  • size:大小
  • linetype:线的类型
  • lineend:线的末尾的风格(round, butt, square)
  • arrow :箭头规格,详见grid::arrow()
  • family:字体名称
  • face :字体类型,有效值是"plain", "italic", "bold", "bold.italic"
  • hjust 、vjust:水平和垂直调整,范围是[0,1]
  • angle :角度,范围是[0,360]

因为Theme()中的美学属性太多,ggplot2预约义了几个主题:

  • theme_gray():灰色背景和白色网格线
  • theme_bw():经典的暗光主题,
  • theme_linedraw():在白色背景上只有各类宽度的黑色线条的主题,让人联想到线条图。
  • theme_light():具备浅灰色线条和轴的主题,以引导更多关注数据。
  • theme_dark():具备黑色色线条和轴的主题,以引导更多关注线条。
  • theme_minimal():没有背景注释的简约主题。
  • theme_classic():具备经典外观的主题,具备x和y轴线,没有网格线。
  • theme_void():空主题

 

参考文档:

ggplot2 Reference

Aesthetic specifications