使用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預約義了幾個主題:
參考文檔: