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

相關文章
相關標籤/搜索